Add, Delete, Get ComboBox item in MFC

Here you can talk about C++ And C# And Other languages programming.

Add, Delete, Get ComboBox item in MFC

Postby BattleStar-Galactica » Sat Jun 09, 2007 3:26 am


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 :D
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.
Attachments
ComboBox.zip
(2.21 MiB) Downloaded 98 times
Last edited by BattleStar-Galactica on Sat Jun 09, 2007 3:31 am, edited 1 time in total.
Big math problem
2 - 1 = 0 = without you I'm nothing
User avatar
BattleStar-Galactica
imFiles Master
imFiles Master
 
Posts: 565
Joined: Tue Sep 20, 2005 12:19 am
Location: safest place to hide

Postby BattleStar-Galactica » Sat Jun 09, 2007 3:28 am

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Attachments
3.JPG
3.JPG (171.83 KiB) Viewed 1153 times
2.JPG
2.JPG (151.64 KiB) Viewed 1151 times
1.JPG
1.JPG (126.93 KiB) Viewed 1152 times
Big math problem
2 - 1 = 0 = without you I'm nothing
User avatar
BattleStar-Galactica
imFiles Master
imFiles Master
 
Posts: 565
Joined: Tue Sep 20, 2005 12:19 am
Location: safest place to hide


Return to C++, C# And Others Programming

 


  • Related topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 0 guests