Click here to Skip to main content
15,885,878 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So i need to handle any change in a WinAPI combobox (its' type = Simple (you can modify the entry in edit box)). Everything is fine, except it always prints previous text, not the current.

C++
void onCommand(WPARAM wparam, LPARAM lparam)
{
    wchar_t str[100] = {0};
    ComboBox_GetText(GetDlgItem(mainWnd, IDC_COMBO1), str, 100);
    avPrint << str << av::endl;

    switch(LOWORD(wparam))
    {
        case IDCANCEL:     EndDialog(mainWnd, 0);   break;
    }
}


What am i doing wrong here?

What I have tried:

1) i've tried to print when ANY command occurs and i still get the previous edit values.
2) If i try to get text later (for example via mouse click) then the text is correct - window procedure code below:

C++
case WM_LBUTTONDOWN: onCommand(0, 0);            return true; // prints actual value
case WM_COMMAND:     onCommand(wparam, lparam);  return true; // prints previous value
Posted
Updated 7-Aug-21 22:24pm
v3
Comments
Avtem 7-Aug-21 12:28pm    
The only way i could get the current text was:
1) Get Edit handle of the combobox
2) Subclass the edit control
3) Get text on WM_PAINT (WM_SETTEXT gives previous results as well...)
Rick York 7-Aug-21 14:02pm    
Have you tried catching the EN_CHANGE message and looking for the handle of the combobox's edit control?

I never use this kind of combobox so I am unfamiliar with it.
Avtem 8-Aug-21 14:12pm    
Yep, it does seem to work, but i think i will stick with "intended" way to use it (if i understood it correctly).
Thank you!
Richard MacCutchan 8-Aug-21 4:21am    
That is the wrong way to get data from a control. You should always use the control's notification messages which tell you when the data or selection changes.
Avtem 8-Aug-21 12:17pm    
But that is my question - what is the right way to get data from the control?

1 solution

You should study Combo Box (Windows Controls) - Win32 apps | Microsoft Docs[^] which explains how to use the control.
 
Share this answer
 
Comments
Avtem 8-Aug-21 14:09pm    
Wow. i found how to do this by accident... Turns out if you want to handle combobox changes you have to handle CBN_EDITCHANGE which is not sent when user selects an item in the listbox and handle CBN_SELCHANGE, but not use WM_GETTEXT this time. They decided that programmer must retrieve Edit text by getting item in the listbox by index sent with CBN_SELCHANGE notification.

This is so confusing to me. Why just don't send CBN_EDITCHANGE for any change in the edit?
Richard MacCutchan 9-Aug-21 3:47am    
If you think this is a bad design then you should tell Microsoft, it's their product.
Avtem 9-Aug-21 4:00am    
Oh, there are more important things that i told them about, but they do not anything about it (Crashes in Visual Studio).

Anyway - did i understand how to use it properly?
Richard MacCutchan 9-Aug-21 4:13am    
"did i understand how to use it properly?"
I don't know, you need to test your code to find out.
Avtem 9-Aug-21 4:27am    
Well, it works perfectly.
Maybe i could not find the answer using Google because others understood it right away

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