Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GetFocus()->GetDlgCtrlID() can't get the ID of focused combox

1,058 views
Skip to first unread message

Cui Sheng

unread,
Jul 20, 2003, 4:08:20 AM7/20/03
to
There are 3 comboxes in a dialog, this function GetFocus()->GetDlgCtrlID()
returns the same value whether IDC_COMBO1 is focused or IDC_COMBO2 is
focused. How do I know which is focused?

Thanks in advance.

Cui Sheng
shen...@hotmail.com


Ajay Kalra

unread,
Jul 20, 2003, 4:21:03 AM7/20/03
to
> returns the same value

How do you know if thats not the case? IOW, how do you know that the focus
is with a particular combobox? Are you calling SetFocus or simply tabbing?

Instead of callign GetDlgCtrlID, see if it matters if you test the window
handle returned by GetFocus with each of the CComboBox objects?

--
Ajay Kalra [MVP - VC++]
ajay...@yahoo.com


"Cui Sheng" <shen...@hotmail.com> wrote in message
news:uX4cpapT...@TK2MSFTNGP12.phx.gbl...

Cui Sheng

unread,
Jul 20, 2003, 5:11:23 AM7/20/03
to
Thanks. Pls see the following code:
BOOL TestDlg::PreTranslateMessage(MSG *pMsg)
{
if(pMsg->message == WM_KEYDOWN)
{
if(pMsg->wParam == VK_RETURN)
{
UINT nID = GetFocus()->GetDlgCtrlID();
switch( nID)
{
case IDOK:
OnOK();
break;
case IDC_EDIT1:
AfxMessageBox("IDC_EDIT1");
break;
case IDC_COMBOX1:
AfxMessageBox("IDC_COMBOX1");
break;
case IDC_COMBOX2:
AfxMessageBox("IDC_COMBOX2");
break;
default:
CString s;
s.Format("default:%d",nID);
AfxMessageBox(s);
break;
}
}
}
return CDialog::PreTranslateMessage(pMsg);
}

If the focus is in the IDC_EDIT1, push the Enter key, it display message
"IDC_EDIT1"
If the focus is in the IDC_COMBOX1, push the Enter key, it should display
message "IDC_COMBOX1",
but in fact it display "Default : 1001"

Do you know why?

Thanks in advance.

Cui Sheng
shen...@hotmail.com

"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:O$HicfpTD...@TK2MSFTNGP11.phx.gbl...

Jeff Partch

unread,
Jul 20, 2003, 5:15:31 AM7/20/03
to
"Cui Sheng" <shen...@hotmail.com> wrote in message
news:uX4cpapT...@TK2MSFTNGP12.phx.gbl...
> There are 3 comboxes in a dialog, this function
GetFocus()->GetDlgCtrlID()
> returns the same value whether IDC_COMBO1 is focused or IDC_COMBO2 is
> focused. How do I know which is focused?

If its a CBS_DROPDOWN style combobox, then GetFocus will return the
handle to its child edit control. You'll have to do something with
GetParent.
--
Jeff Partch [VC++ MVP]


Ajay Kalra

unread,
Jul 20, 2003, 5:23:14 AM7/20/03
to
Thats right. And I think ID is same(101??) for all these controls.

--
Ajay Kalra [MVP - VC++]
ajay...@yahoo.com


"Jeff Partch" <je...@mvps.org> wrote in message
news:u1kT59pT...@TK2MSFTNGP12.phx.gbl...

joey

unread,
Jul 20, 2003, 9:15:07 AM7/20/03
to
try this (note this is for a CFormView, change to your baseclass)

BOOL TestDlg::PreTranslateMessage(MSG* pMsg)
{
if( (pMsg->message == WM_KEYDOWN) && (pMsg->wParam == VK_RETURN) )
{
if(pMsg->hwnd == GetDlgItem(IDC_EDIT1)->GetSafeHwnd())
{
AfxMessageBox("IDC_EDIT1");
return; // no further processing required!
}
else
if(pMsg->hwnd == GetDlgItem(IDC_COMBOX1)->GetSafeHwnd())
{
AfxMessageBox("IDC_COMBOX1");
return; // no further processing required!
}
else
if(pMsg->hwnd == GetDlgItem(IDC_COMBOX2)->GetSafeHwnd())
{
AfxMessageBox("IDC_COMBOX2");
return; // no further processing required!
}
}
// message not for us, do default:
return CFormView::PreTranslateMessage(pMsg);
}

Try to use controll varibles instead of GetDlgItem, to do this, assign a
control var to the control, then change the
GetDlfItem(...)->GetSafeHwnd()
to
c_myControlVar.GetSafeHwnd()


Joe
#

"Cui Sheng" <shen...@hotmail.com> wrote in message

news:uPQN69p...@TK2MSFTNGP10.phx.gbl...

Cui Sheng

unread,
Jul 20, 2003, 9:52:53 PM7/20/03
to
Thank you, It works.

"Jeff Partch" <je...@mvps.org> wrote in message
news:u1kT59pT...@TK2MSFTNGP12.phx.gbl...

0 new messages