Solved

MFC combobox in 5.0

Posted on 1998-10-29
13
836 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 115
c++ syntax question 9 51
HTTPSendRequest with WinINet delays on first call 11 28
FMX TEdit KeyUp handler detecting  "enter" key 4 11
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

791 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