• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 895
  • Last Modified:

MFC combobox in 5.0

I am writing some code to add items to a combobox that I have specified to be a 'dropdown list' type. The problem is that when I execute the program, nothing is listed or drops down at all. I have looked on the Visual C++ 5.0 help books, and from what I see I am doing this correctly. I first 'visually' created a combo box on a form, then I went to the class wizard and added a variable named m_Month of type control clistbox. My code looks like this:

m_Month.InsertString(0, "January");
m_Month.InsertString(1, "February");
m_Month.InsertString(2, "March");
m_Month.InsertString(3, "April");
m_Month.InsertString(4, "May");

and this continues for each month. I placed this code on the forms wm_initialize section. No errors when compiled. I did a similar thing for a list box and got it to work perfectly.
Any suggestions?
0
backdraf
Asked:
backdraf
1 Solution
 
backdrafAuthor Commented:
Edited text of question
0
 
Answers2000Commented:
Your error is probably #3 of this:-

1. I hope you use CComboBox for the combo control.

2. I think you mean OnInitDialog (WM_INITDIALOG)

3. Make sure the combo area has been sized ( I think this is your mistake).  Go into the resources, click on the arrow of the combo, drag the "ghost" rectangle to the size needed.  Most people new to Visual C are fulled by this (and set the drop down area to nothing).

4. If you want months to be in chronological (Jan, Feb, March) rather than alphabetic order, you should also turn off the sort style on the combo box in the resources.  Having said that InsertString ignores the sort style, but it ain't a good idea to mix this up.

BTW - why not simply use
m_mMonth.AddString( "Jan" ) ; // which always adds to the end of the list

If you are still having problems, catch the return value of InsertString (it returns an int) - atleast for debugging purposes.  if you are getting a negative value back then it's a code error (you may be initializing the combo box before it's ready -  but I don't think so), if you get 0 or greater it's probably a resource problem = item #3 )
0
 
backdrafAuthor Commented:
It didn't work. The Combo Box is not able to be resized in the vertical direction, only the horizontal(which is wide enough to accomodate the longest month). Here is my code again:


BOOL CAlarm1dlg::OnInitDialog()
{
      CDialog::OnInitDialog();
      
      // TODO: Add extra initialization here
      m_Day.InsertString (0,"Monday");
      m_Day.InsertString (1,"Tuesday");
      m_Day.InsertString (2,"Wednesday");
      m_Day.InsertString (3,"Thursday");
      m_Day.InsertString (4,"Friday");
      m_Day.InsertString (5,"Saturday");
      m_Day.InsertString (6,"Sunday");
      m_Day.SelectString(0,"Monday");

      m_Month.InsertString (0,"January");
      m_Month.InsertString (1,"February");
      m_Month.InsertString (2,"March");
      m_Month.InsertString (3,"April");
      m_Month.InsertString (4,"May");
      m_Month.InsertString (5,"June");
      m_Month.InsertString (6,"July");
      m_Month.InsertString (7,"August");
      m_Month.InsertString (8,"September");
      m_Month.InsertString (9,"October");
      m_Month.InsertString (10,"November");
      m_Month.InsertString (11,"December");
      m_Month.SelectString(0,"January");
      return TRUE;  // return TRUE unless you set the focus to a control
                    // EXCEPTION: OCX Property Pages should return FALSE
}

I have tried creating a new combo box and leaving all the defaults as is and using the same code as above on it and it did not work either. m_Day is for a List Box and m_Month is for the combo box. I tried all of your suggestions, thanks, same problem though. I tried debuging the app but couldn't find anything. I ran across the events for the combo box and was wondering do I have to handle the cbn_dropdown message?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
thresher_sharkCommented:
Does the m_Day drop down box (if that's what it is) work?
0
 
backdrafAuthor Commented:
I've also tried .AddString as well and it does nothing for the situation. One interesting thing is that:

m_Month.SelectString(0,"January");

does indeed cause the combo box to show January in the combo box, but when you try to drop the list down, nothing happens. I am asuming a combo box is nothing more than a list box 'rolled' up. So I don't see why I'm having such a hard time with this combo box.

0
 
backdrafAuthor Commented:
m_day is a list box which does not have a scroll bar or any kind of a drop down menu. Its just a recesed box with all of the days visible at a glance.
0
 
backdrafAuthor Commented:
m_day is a list box which does not have a scroll bar or any kind of a drop down menu. Its just a recesed box with all of the days visible at a glance.
0
 
backdrafAuthor Commented:
m_day is a list box which does not have a scroll bar or any kind of a drop down menu. Its just a recesed box with all of the days visible at a glance.
0
 
backdrafAuthor Commented:
Solved! I had to change the style of the combo box to 'simple', then resize it to the length I wanted to make the vertical scroll bar to be when the app ran and then change the style back to 'dropdown list'. Perhaps there is a function to do this for me but this works just as well.
0
 
pagladasuCommented:
I guess Answer2000 should get the points. He suggested everything correct except for changing the style and resizing.
Also when you first insert a combobox into the dialog box, you can resize it vertically as well. However, once inserted, you cannot change its vertical size unless the style is first changed.
0
 
Answers2000Commented:
STYLES

DropDown = An edit box plus a drop box (user can pick one of your choices or type something else)

DropDownList =  a drop box (user must pick one of your choices)

Simple  = edit + drop box (always dropped)  (user can pick one of your choices or type something else)

For months you should be using DropDownList style

Incidentally you drop down and dropdown/dropdown list are sized vertically - click on the little arrow on the RHS of the combo control in the resource editor.  You will see a "ghost" box.  Size this.  At runtime, the size of the dropped is area is whichever is less of this ghost area or the size to accommodate all items.  I tried to say this in my original comment #'3 -  did you understand that?

CHANGING STYLE

You can't flip the style between these at runtime.  If you need to appear to change the style at runtime, create 2 combo boxes at the same position with different styles, hide/disable one, enable/show the other,  then do it the other way round to switch to the other combo   Example :-

// disable m_combo1, enable m_combo2

m_combo1.ShowWindow( SW_HIDE ) ;
m_combo1.EnableWindow(FALSE) ;

m_combo2.ShowWindow( SW_SHOW ) ;
m_combo2.EnableWindow(FALSE) ;


QUESTION ??
Shall I answer as pagladasu suggests that I should


0
 
migelCommented:
You must select string in the combobox to show it:
..
m_Month.InsertString(...);
...
..
// show first string in control
m_Month.SetCurSel(0);
}
0
 
RONSLOWCommented:
To change the size of a combo box in the resource/dialog editor:

To change the width of the combo, simply drag with the mouse (the height is that of the edit control -- use SetItemHeight with item code -1 in code to change this).

To change the height, FIRST click on the dropdown button in the resource/dialog editor, then drag to the required size (this width of the combo drop-list is the same as the edti box -- use SetDroppedWidth in code to change the width of the combo)

Note that if you create a combobox in code, the size you specify is that of the combo+dropdown.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now