ComboBox AddString()

I used the vc++ resource manager to create a combo box on the program's main form. I would like to use AddString() at run time to fill the combo box with a list of valid selections. But, the AddString() function requires a (CComboBox*). The resource manager created the combo box, but never told me the name of the variable, only the name of the ID (in this case, the ID is IDC_VAR_NAME). How do you make the connection from ID to (CComboBox*)


Answer this question

ComboBox AddString()

  • stanley lippman

    I have the same problem than Sergey.

    And it's really upset, this control worked so well before.


  • V VICTOR

    If you are getting an assert, chances are that you are attempting to access the CComboBox* before the underlying combobox control has got created. Where in your code are you trying to access the combobox

  • GregE

    I finally have the answer.

    First and foremost, the ComboBox was working, but it was not letting me see it was working. If the data resource is null, and if you do not graphically enlarge the drop-down area on your main form (this is when you are adding controls to the form using the toolbox), you will never see anything you put in AddString(). It is very easy to think that you can not graphically enlarge the drop down area, since you must click on the arrow at the right side of the window to activate the control to enlarge the drop down area. If you click anywhere else, it will never show you the control point necessary to drag to do the enlargement. The key to knowing this is happening is to look for a heavy line immediately below the combo box. I think that heavy line represents both the upper and lower borders of the not-large-enough drop down area.

    I also found out that if you create a generic combo box with the toolbox, and you set the "Owner Draw" resource to either Fixed or Variable, you will get an "assertion failed" error from winctrl1.cpp when you try to call AddString().

    So, I'm back in business with my combo box. I appreciate the help.



  • Rim van Wersch


    I also have been using VS2005 and not being able to set the drop down size for CComboBox. It is quite a pain - the items exist but there is no way in the designer to set the drop down height! At least they could've given it a decent default value other than zero. Maybe it's in SP1... I really should install that!

    Anyway, the code that fixes it for me:

    Code Block

    CRect r;
    m_cmbCombo.GetClientRect(&r);
    m_cmbCombo.SetWindowPos(NULL, 0, 0, r.right, 200, SWP_NOMOVE | SWP_NOZORDER);



    Thanks to KTM (http://www.mulveyfamily.com/Katy/VisualC/Code/cbresize.html)

    HTH
    Aranda

  • wilsonjan

    I found the solution for my CCombobox problem.

    It missed this line at the begining my OnInitDialog function :

    CDialog::OnInitDialog();

    Insert the code after that line and it work !!


  • John Dieter

    seattleboatguy wrote:
    I used the vc++ resource manager to create a combo box on the program's main form. I would like to use AddString() at run time to fill the combo box with a list of valid selections. But, the AddString() function requires a (CComboBox*). The resource manager created the combo box, but never told me the name of the variable, only the name of the ID (in this case, the ID is IDC_VAR_NAME). How do you make the connection from ID to (CComboBox*)

    You can either use a DDX variable - right click the combobox in the resource editor and add a control variable.

    Or you can use GetDlgItem(IDC_VAR_NAME), cast the returned CWnd* to a CComboBox* and do what you want with it.



  • XP is self aware

    Hi Nishant. I liked your GetDialogItem() approach, but I found that neither of your 2 ideas worked for me.

    I added a control variable named george, but the call to george.Addstring() causes an assert error at run time.

    I also tried the following logic with GetDialogItem(), but no string was added to the ComboBox:

    CComboBox* pvarComboWindow;

    LPCTSTR fff = "hello";

    pvarComboWindow = (CComboBox*) GetDlgItem(IDC_VAR_NAME);

    int ggg = CB_ERR; // debugging

    int hhh = CB_ERRSPACE; // debugging

    int iii = pvarComboWindow->AddString(fff);

    // george.AddString(fff);

    I don't know if this will help, but here are the resources I'm using for the ComboBox:

    Accept Files False

    Auto False

    Client Edge False

    Data

    Disable No Scroll False

    Disabled False

    Group False

    Has Strings True

    Help ID False

    ID IDC_VAR_NAME

    Left Scrollbar False

    Lowercase False

    Modal Frame False

    No Integral Height False

    OEM Convert False

    Owner Draw Variable

    Right Align Text False

    R to L Read Order False

    Sort True

    Static Edge False

    Tabstop True

    Transparent False

    Type Dropdown

    Uppercase False

    Vertical Scrollbar True

    Visible True



  • Eric Lewandowski

    I have the same problem. I'm using Visual Studio 2005 for an MFC application. AddString() and InsertString() do not work in both CListBox and CComboBox classes.

    With members properly defined for DDX and the method correctly called within OnInitDialog() of a modal dialog, I contiuiously get a debug assertion. Tracing that assertion leads to a failure of IsWindow() call on the control handler somewhere within MFC. In release session, as well as in runtime, there's no assersion but the control appears empty (no strings added).

    GetDlgItem() does not create an assertion but there're still no lines added to the control.

    If I recreate the dialog, sometimes Listbox works, and sometimes Combobox shows just the first line, and I cannot see any pattern.

    How could we expect such a problem in such a trivial case from the new version of a tool that has been working reliably for 12 years

    Sergey


  • ComboBox AddString()