Sign in to follow this  

Checking what item is selected in a CBS_DROPDOWNLIST

This topic is 2543 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
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
Quote:
Original post by Erik Rufelt
How do you create the combo box?
Show your entire WndProc and creation code.


I create the combo through a resource file:


COMBOBOX IDC_COMBO1, 104, 34, 60, 78, WS_VSCROLL | CBS_DROPDOWNLIST | CBS_HASSTRINGS


I then give it it's strings like this:

case WM_INITDIALOG:
ControlHwnd = hDlg;
for(int x=0;x<79;x++)
{
SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_ADDSTRING, 0, (LPARAM)Keyboard[x].c_str());
}
break;


Then the code I added from you is this:

case WM_COMMAND:
switch(wParam)
{
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(int((Keyboard[itemIndex])[1])).c_str(), _T("Key:"), 0);
Variable = int((Keyboard[itemIndex])[1]);
}
break;


I also have other controls in similar circomstances in the WM_COMMAND that work fine, for example:


case IDC_CHECKBOX2:
//Do stuff here
break;



Please Help,

Thanks In Advance,

Joe

Share this post


Link to post
Share on other sites
Quote:
Original post by adeyblue

case WM_COMMAND:
switch(wParam)
{

This is wrong, it should be

case WM_COMMAND:
switch(LOWORD(wParam))
{


Ah. I didn't think it would make a difference excluding this.
Works now :D Thanks

Share this post


Link to post
Share on other sites

This topic is 2543 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this