Jump to content
  • Advertisement
Sign in to follow this  
Joesavage1

Checking what item is selected in a CBS_DROPDOWNLIST

This topic is 2707 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I've just created an application in which the user must use a combobox to decide on which key they want to use for an operation. I've added the different options by using a for loop and this line:

SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_ADDSTRING, 0, (LPARAM)Keyboard[x].c_str());


However, now the options are added I need to find which item the user has selected.
I've looked around the web for hours and tried various things, but none worked.


Please Help,

Regards,

Joe

Share this post


Link to post
Share on other sites
Advertisement
You can use the ComboBox_GetCurSel Macro, or use SendMessage to send CB_GETCURSEL to the control.

The ComboBox Control Reference lists all messages and everything else you need to know about combo boxes.

You get notification of a change with the WM_COMMAND message, and HIWORD(wParam) will be CBN_SELCHANGE.


WndProc(...) {
switch(msg) {
case WM_COMMAND:
switch(LOWORD(wParam)) {
case IDC_COMBO1:
if(HIWORD(wParam) == CBN_SELCHANGE) {
int itemIndex = ComboBox_GetCurSel(GetDlgItem(hDlg, IDC_COMBO1));// or (HWND)lParam is also the control window here
}
break;
}
break;
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by rhummer
CB_GETCURSEL is the message you are looking for.

it will return CB_ERR, -1, if nothing is selected.


I have come across this - however I didn't know how to tell which item was selected from inside this message (I would guess it's the wParam or something).
If I am correct in saying that something indicating is in the wParam - then what is it that indicates what is selected in the combobox?

EDIT: Didn't see "Erik Rufelt's" post when posting the above.
What does "ComboBox_GetCurSel" get me that I can compare it to?

Share this post


Link to post
Share on other sites
The return value from SendMessage is the index of the item selected, just pass NULL for wParam and lParam as they are not used.

You can also follow Erik Rufelt's suggestion too.

Share this post


Link to post
Share on other sites
Quote:
Original post by Joesavage1
What does "ComboBox_GetCurSel" get me that I can compare it to?


The index of the item. The first item is 0, the second 1, etc. If you want the text of the item I think you can use ComboBox_GetLBText. Check through the documentation, there are functions for pretty much anything you can want to do with your combobox.

Share this post


Link to post
Share on other sites
The itemIndex is perfect since I'm using an array to create all of the dropdown items!
What header file is this ComboBox_GetCurSel macro in?
I get the error: 'ComboBox_GetCurSel': identifier not found

Share this post


Link to post
Share on other sites
If you check the bottom of an MSDN page the header and library is always listed there. For the combo-box macros it seems to be Windowsx.h. If you use SendMessage then you should only need the normal Windows.h.

Share this post


Link to post
Share on other sites
Woke up this morning and tested this out - unfortunately it didn't work.
I put the following into my WM_COMMAND switch statement (which works fine for the all the buttons and other things):


case IDC_COMBO1:
MessageBox(0, _T("Combo1!"), _T("Combo1!"), 0);
if(HIWORD(wParam) == CBN_SELCHANGE){
MessageBox(0, _T("CBN_SELCHANGE!"), _T("CBN_SELCHANGE!"), 0);
int itemIndex = ComboBox_GetCurSel(GetDlgItem(hDlg, IDC_COMBO1));
MessageBox(0, ConvertToString(itemIndex).c_str(), _T("Item Index:"), 0);
MessageBox(0, ConvertToString(CharVirtualCode((Keyboard[itemIndex])[1])).c_str(), _T("Key:"), 0);
Variable = CharVirtualCode((Keyboard[itemIndex])[1]);
}
break;


All the MessageBoxes are there for debbugging purposes.
When I change the combobox value I don't get any of the MessageBoxes which would indicate that there is something wrong with the above code (remember that the WM_COMMAND works fine for everything else in the program).


Please Help,

Regards,

Joe

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!