Combo box default selection

I've placed a combo box on my form and populated it with a handful of options like this:

1
2
3
4
5
SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)L"Option 1");
SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)L"Option 2");
SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)L"Option 3");
SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)L"Option 4");
SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)L"Option 5");


I've managed to get away without making an actual class for this combo box and it might be a good idea for me to make one (but I'm very fuzzy on how exactly to do it). How can I set one of those selections as automatically selected (is this called "focus" perhaps?) and also set the combo box to where the user can't type anything into it? In short, I'd like to set it up so it is basically a dropdown box. I tried to find this in the tool box (Visual Studio 2010) but the only thing I found was a combo box.
Last edited on
Send CB_SETCURSEL message to set a default selection
SendMessage(hComboBox, CB_SETCURSEL, (WPARAM)item_idx,0 );

http://msdn.microsoft.com/en-us/library/windows/desktop/bb775899(v=vs.85).aspx
Thank you! The links to stuff like that are (slowly!) starting to make more sense to me now that I've been struggling with them for a while. I've tried looking this sort of thing up but when my search turns up only vague information where it only says something like "Use this" but doesn't tell me HOW to do it, I am none the wiser. But code like the one you provided has been helping me use those resources more easily because I'm getting used to seeing them in action.

Is there a way to set it so the user can't alter anything in the combo box? The above code sets the combo box the way I want but I can still edit it.
Give your ComboBox control the window style CBS_DROPDOWNLIST if you want it be a drop-down static control rather than a drop-down edit control.

(i.e. a drop-down list is just a read-only combo-box.)

In Visual Studio, it's the "Type" property of the ComboBox you need to set; it needs to be "Drop List " (rather than "Dropdown" or "Simple".)

Andy

Combo Box Styles
http://msdn.microsoft.com/en-us/library/windows/desktop/bb775796%28v=vs.85%29.aspx
Last edited on
That did it! I'd tried that one before but it didn't do what I want and I think it was because I didn't have a default value set for it.
Topic archived. No new replies allowed.