hi

im writin a simple GUI project usin C++ (Dialog). i have an input file with a list of foods. my goal is to display the list of foods in a ComboBox and then Add some new food and Delete some.
But i just cant put the list to a ComboBox (dropdown list style). i used addString method. it didnt work. can anybody just tell me the steps to add a list to a ComboBox using C++?
Thanks :)

Dani AI

Generated

A concise, practical follow-up that fills gaps in the replies from and and shows exactly where things commonly go wrong.

First, make sure the combo is a control variable (not a CString value). Wire it with DDX_Control in your dialog class before you try to add items:

// header: CComboBox m_combo;

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_COMBO_FOODS, m_combo);
}

Populate the combo after the control exists (OnInitDialog for a resource control, or after Create() if you make it at runtime). Example using MFC file I/O:

BOOL CMyDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    CStdioFile file;
    CString line;
    if (file.Open(_T("foods.txt"), CFile::modeRead | CFile::typeText))
    {
        while (file.ReadString(line))
            m_combo.AddString(line);
        file.Close();
    }
    m_combo.SetCurSel(0);
    return TRUE;
}

Why the dropdown might not appear on the down-arrow:

  • The combo’s resource height controls the drop-list height. In the dialog editor, make the control taller to increase the dropdown area (what suggested). For runtime creation, the RECT you pass to Create() sets that height:
    CRect r(10,10,200,150); // height includes drop list
    m_combo.Create(WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST|WS_VSCROLL, r, this, IDC_COMBO_FOODS);
  • To open programmatically: m_combo.ShowDropDown(TRUE); or m_combo.SendMessage(CB_SHOWDROPDOWN, TRUE, 0);

Add/Delete examples and saving back to file:

// add from edit
CString s; GetDlgItemText(IDC_NEWFOOD,s); if(!s.IsEmpty()) m_combo.AddString(s);

// delete selected
int i = m_combo.GetCurSel();
if (i!=CB_ERR) m_combo.DeleteString(i);

// save
CStdioFile out; if(out.Open(_T("foods.txt"), CFile::modeCreate|CFile::modeWrite|CFile::typeText))
{
  for(int j=0;j<m_combo.GetCount();++j){ m_combo.GetLBText(j,s); out.WriteString(s+_T("\n")); }
  out.Close();
}

Quick troubleshooting checklist: confirm DDX_Control mapping and correct IDC, populate after control exists, check return values (AddString can return CB_ERR/CBERRSPACE), ensure the combo has the correct CBS* style, and verify nothing in your dialog (accelerators/PreTranslateMessage) is swallowing arrow keys.

hi

im writin a simple GUI project usin C++ (Dialog). i have an input file with a list of foods. my goal is to display the list of foods in a ComboBox and then Add some new food and Delete some.
But i just cant put the list to a ComboBox (dropdown list style). i used addString method. it didnt work. can anybody just tell me the steps to add a list to a ComboBox using C++?
Thanks :)

Do you have a variable of type control? In other words, it would be a variable defined something like as CComboBox m_Box. If you have a vaiable like this, you can use AddString. That would look like m_Box.AddString(...) in an MFC application.
You can, of course, also have a CString variable associated with the edit box part of the control. Then it would just be a matter of grabbing the CString variable and adding it to the list with AddString.

Do you have a variable of type control? In other words, it would be a variable defined something like as CComboBox m_Box. If you have a vaiable like this, you can use AddString. That would look like m_Box.AddString(...) in an MFC application.
You can, of course, also have a CString variable associated with the edit box part of the control. Then it would just be a matter of grabbing the CString variable and adding it to the list with AddString.

thanks. now i have the list in the combobox, BUT the list doesnt come down when i hit down arrow. i am trying to use m_combo.create but i dont know what parameters i should use, for example .. CWnd *PparentWnd.
or is there any way i can make the combobox to drop the list when i hit down arrow?
thanks in advance.

thanks. now i have the list in the combobox, BUT the list doesnt come down when i hit down arrow. i am trying to use m_combo.create but i dont know what parameters i should use, for example .. CWnd *PparentWnd.
or is there any way i can make the combobox to drop the list when i hit down arrow?
thanks in advance.

=======================

what u need to do is during the design time itself after placing the combobox, then click on the arrow. u will see a rectangle box in the background , then drag it , to wahtever lenght u need to use, start using it and enjoy coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.