Skip to content

Add, Delete, Get ComboBox item in MFC

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #191196

    You have to read my previous post to able read this one. it will be quite short for this tuto.

    1) start vc++6 and name it ComboBox
    2) drag a comboxbox and a button to your form, see image 1

    we need to declare a variable type CComboBox, Do you remember where to declare a global variable for our app; for this project click on FileView and double click on ComboBoxDlg.h. scroll down right after DECLARE_MESSAGE_MAP(), enter the code bellow

    public:
    CComboBox m_MyComboBox;

    see image 2

    we need to attach our variable m_MyComboBox with our ComboBox that we added to our form(Dialog app). How to do that, follow step 3

    3) double click on ComboBoxDlg.ccp, scroll down you will see a procedure call something like this

    void CComboBoxDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CComboBoxDlg)
    // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
    
    }

    in this procedure we will attach our m_MyComboBox with our ComBobox, right after the line //}}AFX_DATA_MAP, add follow code

    DDX_Control(pDX,IDC_COMBO1,m_MyComboBox);

    see image 3

    now we’re going to add some value to that combobox

    4) on button click procedure
    – to add some value to this combobox, simply do this >>

    CString str="some item";
    m_MyComboBox.AddString(str);

    – get some current selected item

    CString str;
    int idx = m_MyComboBox.GetCurSel();
    m_MyComboBox.GetLBText( idx, str );

    str will content the current item selected

    – and to delete current item selected

    int idx = m_MyComboBox.GetCurSel();
    m_MyComboBox.DeleteString(idx);

    you’re done 😀
    I attach a demo project for this tuto, in the demo project I show you also implement CEdit class to deal with edit control that I didn’t show in previous post.

    #191197

    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.