Solved

MFC combobox in 5.0

Posted on 1998-10-29
13
819 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
13 Comments
 

Author Comment

by:backdraf
Comment Utility
Edited text of question
0
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
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
Comment Utility
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
 
LVL 6

Expert Comment

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

Author Comment

by:backdraf
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:backdraf
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now