Solved

MFC combobox in 5.0

Posted on 1998-10-29
13
856 Views
Last Modified: 2008-03-17
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
Comment
Question by:backdraf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 

Author Comment

by:backdraf
ID: 1176507
Edited text of question
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1176508
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
 

Author Comment

by:backdraf
ID: 1176509
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:thresher_shark
ID: 1176510
Does the m_Day drop down box (if that's what it is) work?
0
 

Author Comment

by:backdraf
ID: 1176511
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
 

Author Comment

by:backdraf
ID: 1176512
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
 

Author Comment

by:backdraf
ID: 1176513
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
 

Author Comment

by:backdraf
ID: 1176514
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
 

Author Comment

by:backdraf
ID: 1176515
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
 
LVL 4

Expert Comment

by:pagladasu
ID: 1176516
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
 
LVL 8

Expert Comment

by:Answers2000
ID: 1176517
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
 
LVL 12

Expert Comment

by:migel
ID: 1176518
You must select string in the combobox to show it:
..
m_Month.InsertString(...);
...
..
// show first string in control
m_Month.SetCurSel(0);
}
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
ID: 1176519
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question