Click here to Skip to main content
15,887,936 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am not able to get handle of CComboBox.

C++
CComboBox *pComboBox = reinterpret_cast<ccombobox>(GetDlgItem(IDC_COMBO1));


I checked value of pComboBox is null.

I am surprised, The same line of code is working fine in my other small application but here it fails.

Please provide your input? I need to get handle to combo Box and use the handle.

Regards,
Joy

What I have tried:

C++
CComboBox *pComboBox = reinterpret_cast<ccombobox>(GetDlgItem(IDC_COMBO1));
Posted
Updated 15-Feb-16 5:57am
v2
Comments
nv3 15-Feb-16 11:24am    
Check the ID of your combo box. Is it really IDC_COMBO1. Other possible issues:

- code is not executed from within a member function of the window that contains the combo box

- you are executing the code at a stage where the controls of the dialog have not been created yet
Richard MacCutchan 15-Feb-16 11:29am    
Your cast statement does not make sense, and should not even compile.

1 solution

The line of code you've presented isn't getting the handle to anything - it's apparently trying to recover the pointer to a CComboBox MFC wrapper object that's a child of the window that supplies the this pointer the code is executed under.

So...

IF you really want a pointer to the MFC wrapper object NEVER use reinterpret_cast, use static cast.

C++
auto pComboBox = static_cast<ccombobox>( GetDlgItem( IDC_COMBO1 ) );</ccombobox>


IF you actually want the handle use the m_hWnd member of whatever is returned by GetDlgItem, e.g.

C++
auto handle = GetDlgItem( IDC_COMBO1 )->m_Hwnd;


Try those lines of code and see what happens. If neither do what you want perhaps modify your question to reflect what you want to do!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900