Needing help with controls

I am needing some help with controls. Making a form, dragging controls onto it, and doing some VERY BASIC things with it are all I can do. What I'm wanting to do is create a dropdown menu (combo box??) that has items I want in it. I've been able to use this:

SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)"blahblahblah");

But that only gets me one item in my combo box. If I make another line, it ignores the first one and uses the second one.

I'm not sure what to do from here. Looking online has been no help. Many (presumably helpful) individuals respond to others who are asking the same questions I am with solutions that I don't know where to begin to implement or they link to Microsoft's page on that particular class and that documentation leaves me none the wiser.

Can someone help me out with how I can set this up? Do I need a class or can this be done in the WM_INITDIALOG? If I do need a class, how would I set that up? I know just enough programming to fall headlong into a problem like this and get stuck :).
Select the combo box with mouse cursor and use keyboard (up and down arrows) to cycle through items. Items are there but they aren't visible.

If you are using CreateWindowEx function to create the combo box, then increase height of the control (5th parameter tof the function).
I didn't use CreateWindowEx. All I did was drag the control from the toolbox and bravely tried to forge ahead to use the control. I don't see an option to increase the height of the control.
There must be an option to resize the control but I can't help you because don't know what IDE are you using (Visual Studio? something else?)

You can use SetWindowPos function to resize combo box or any other control or window SetWindowPos(hComboBox,0,0,0,width,height,SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE); . Set height to 100 or more and it should work fine
Last edited on
I'm using Visual Studio 2010.

I appreciate the help but it would be good if you could explain some of the how's as well. For example, where would I put the SetWindowPos function? Will I need to adjust the height every time I want to add a new item to the list? So there is no way to do it automatically as items are added? I only plan on having a handful of items available but I may add a couple more later.

Also, is there a way to set the combo box to a default selection and not give the user the option to type anything in? I see those kinds of dropdown boxes in all sorts of programs but didn't find anything beyond a combo box that does that sort of thing.

And for when I finally get this hammered out, how would I be able to use the combo box selection? How would I be able to determine which one is selected?
For example, where would I put the SetWindowPos function?

You can put it anywhere when you need to change size of the control. Since you need to resize the combo box only once, you should put it inside WM_CREATE or WM_INITDIALOG. (WM_CREATE message is used with windows while WM_INITDIALOG is used with dialog boxes)

Will I need to adjust the height every time I want to add a new item to the list?
No. You need to call this function only once. Later, if you'll have even more items, a scroll bar should appear inside combo box, so you shouldn't have any problems

Also, is there a way to set the combo box to a default selection and not give the user the option to type anything in? I see those kinds of dropdown boxes in all sorts of programs but didn't find anything beyond a combo box that does that sort of thing.
It's possible but I am not sure that you can modify combo box style after it is created.

And for when I finally get this hammered out, how would I be able to use the combo box selection? How would I be able to determine which one is selected?


1
2
3
4
5
6
7
8
9
 

 DWORD index=SendMessage(hComboBox,CB_GETCURSEL,0,0); // find out which item is selected

  int str_len=SendMessage(hComboBox,CB_GETLBTEXTLEN,(WPARAM)(index),0); // get text length

  TCHAR *str_buff=new TCHAR[str_len]; // allocate memory buffer to hold the text

 SendMessage(hComboBox,CB_GETLBTEXT,(WPARAM)(index),(LPARAM)(str_buff)); // get item text 


You can also use ComboBox_GetCurSel(), ComboBox_GetLbText() and similar macros which are delcared in windowsx.h instead of SendMessage (even though those macros call SendMessage function, but they require less typing )

For more information, see http://msdn.microsoft.com/en-us/library/windows/desktop/bb775792(v=vs.85).aspx
Last edited on
Thank you! This will give me something to work with :).

If I can't modify a combo box after it is create, is there a way to initially set it up with a default value already selected and no option for users to add to it? This won't need to be done in runtime so if I can initialize it that way, all the better.

I'll give a little background on what I am trying to do in case it may help.

What I am trying to do is create a small program that will rewrite words from one orthography (writing system...linguist stuff :) ) and to the system we are currently using. The dropdown menu will serve to select the source of the words so the program will know which algorithm to use with the input. The default selection will be labeled "UNKNOWN" and will attempt to analyze the input as best it can. That one will probably be a bear to make :).

So if the user selects Soandso as the source of the term they are wanting to have rewritten, the program will use the appropriate function to crunch the word. It isn't anything super complicated...it will just look for certain characters and convert them as needed. For example, some of my sources use the "j" character to represent the "sh" sound. So in those functions, anytime a "j" is found, it will swap it out with an "sh". Not really that hard. I believe the term for this is "string manipulation."

Does that help give a context for what I am after? The user will select a source and that will be fed into the function to tell it which algorithm to use.
Oh, and before I forget, I will want the functions to return possible results to a list box. I say possible results because, for example, in the above post I mentioned that in some sources, the "j" is the "sh" sound. It can also be an "s" sound. So when this happens, I am going to have the algorithm create a new string and the function will continue to work using both strings. Not quite sure how I want to handle that...I'm thinking maybe a string vector array maybe.

Anyway, I haven't been able to find a (somewhat simple!) way to simply feed strings into a list box.
If I can't modify a combo box after it is create, is there a way to initially set it up with a default value already selected and no option for users to add to it? This won't need to be done in runtime so if I can initialize it that way, all the better.
I don't have visual studio but you should find properties box somewhere. There should be an option to select style. You need CBS_DROPDOWNLIST style.

Also, if you want the program to respond instantly after combo box selection changes, you might want to handle CBN_SELCHANGE message ( http://msdn.microsoft.com/en-us/library/windows/desktop/bb775821(v=vs.85).aspx )
I don't have a "style" option in my properties pane but I do have a "type" where I can select "simple", "dropdown" and "drop list". I'd already toyed around with that and the default "dropdown" seems to be the one that does roughly what I want.
One problem solved! I was toying around with the properties and running the program to see what would happen and I noticed the up and down arrows on the right side of the combo box after I clicked the initial down arrow to expand it. I could barely see them since they were mostly cut off due to the size of the box. So I tried clicking them and sure enough, I was cycling through the names! So I plugged in your code:

SetWindowPos(hComboBox,0,0,0,width,height,SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE);

And it works! I put in 100 (as you suggested) for the height. I put another arbitrary 100 in there for the width so I could see the names. Is there a way I can bypass the height argument here and just use the width size in the "drag and drop" dialog window?
I've got more of the functionality working! I want to say thank you for taking the time to help me here. The nudges you've given me have helped me and given me enough information to start more experimenting. I've now been able to get the currently selected combobox item and am able to use that to determine which algorithm to use. Now I have to work with my algorithms and get them working the way I want. I should be able to figure out how to output the results to a listbox now that I have something to work with.

Again, thanks!
I've gotten frustratingly close to adding items to a list box. Like I mentioned above, websites say something along the lines of "Create a CListBox class and use it! Simple!" Again, I am none the wiser LOL. How would I create a class for a control? Is that the same as dragging a control onto a form? Or is coding a class for a control different? If they are different, how do I get at the listbox "object" if I simply dragged the control onto the form. I found one website that said if you do this, you need to create a member variable for it but then dove right into code that was about making a class (which I couldn't get to work) without telling me how to make a member variable for it.

For the purposes of this small program, I'm not wanting to get too complicated with a bunch of classes. If I can get away with doing it in a simple way, that is what I want to do. You know, the whole walk before you run thing :). Anyone know of a simple way I can get at a control that was placed on a form from the toolbox so I can work with it?
I've gotten frustratingly close to adding items to a list box. Like I mentioned above, websites say something along the lines of "Create a CListBox class and use it! Simple!" Again, I am none the wiser LOL. How would I create a class for a control? Is that the same as dragging a control onto a form? Or is coding a class for a control different? If they are different, how do I get at the listbox "object" if I simply dragged the control onto the form. I found one website that said if you do this, you need to create a member variable for it but then dove right into code that was about making a class (which I couldn't get to work) without telling me how to make a member variable for it.
CListBox is a MFC class.MFC (microsoft foundation classes) is a C++ library that allows to handle WinAPI functions (windows, controls...) in object oriented way. You can use MFC if you want but I guess that will only make things more complicated for you because you are struggling with WinAPI functions. I have never used it so I can't tell anything about it.
Back on topic: to add a string to list box, send LB_ADDSTRING meesage
SendMessage(hListBox,LB_ADDSTRING,0,(LPARAM)lpString);

LB_ADDSTRING: http://msdn.microsoft.com/en-us/library/windows/desktop/bb775181(v=vs.85).aspx
ListBox_AddString macro: http://msdn.microsoft.com/en-us/library/windows/desktop/bb856426(v=vs.85).aspx
That line of code was exactly what I needed! Thank you!!
Topic archived. No new replies allowed.