How to create variable for a dynamically created combo box

stp's Avatar, Join Date: Feb 2008
stp
Light Poster
I have written this code and now i have dynamically create combo box.

Code:

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

    SetIcon(m_hIcon, TRUE);
    SetIcon(m_hIcon, FALSE);

    // TODO: Add extra initialization here

 CComboBox *Combo = new CComboBox;
   Combo->Create(WS_CHILD | WS_VISIBLE |
   WS_VSCROLL | CBS_DROPDOWNLIST,
   CRect(10, 50, 100, 150), this, 0x1448);

   Combo->AddString("First");
   Combo->AddString("Second");
   Combo>AddString("Third");

return TRUE; // return TRUE unless you set the focus to a control
}


When a user clicks an item in the combo box to select it, I want to find out what the user clicked.
I am interested in the string that the user selected in the combo box,but i need variable for this dinamically created combo box.
My question is how to create a variable.


I hope you understand what i want to do and i am sorry for my bad English
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Make Combo box pointer class variable and then get the selected item from the combo box variable
stp's Avatar, Join Date: Feb 2008
stp
Light Poster
Thank you, but I have been learning Visual C++, WInApi and MFC for not long time I don't know how to do it.
Can you tell me how to make Combo box pointer class variable and then get the selected item from the combo box variable
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You should then be doing your hands on with C++ because how to put a variable in a class is nothing to do with VC++. The variable pointer you declared in your above code should be moved to CProjectDlg class member variable.
stp's Avatar, Join Date: Feb 2008
stp
Light Poster
This is what I made.

ProjectDlg.h file

Code:
class CProjectDlg : public CDialog
{


	private:
  CComboBox *Combo;
// Construction
public:
	CSixDlg(CWnd* pParent = NULL);	// standard constructor
...
...
...


ProjectDlg.cpp

Code:
BOOL CProjectDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    SetIcon(m_hIcon, TRUE);
    SetIcon(m_hIcon, FALSE);

    // TODO: Add extra initialization here

 Combo = new CComboBox;
  Combo->Create(WS_CHILD | WS_VISIBLE |
  WS_VSCROLL | CBS_DROPDOWNLIST,
  CRect(10, 50, 100, 150), this, 0x1448);

  Combo->AddString("First");
  Combo->AddString("Second");
  Combo->AddString("Third");


return TRUE; // return TRUE unless you set the focus to a control
}

Can you help me further? Or give me a link with this information
hansraj's Avatar, Join Date: Mar 2010
Newbie Member
Quote:
Originally Posted by stp View Post
This is what I made.

ProjectDlg.h file

Code:
class CProjectDlg : public CDialog
{


	private:
  CComboBox *Combo;
// Construction
public:
	CSixDlg(CWnd* pParent = NULL);	// standard constructor
...
...
...


ProjectDlg.cpp

Code:
BOOL CProjectDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    SetIcon(m_hIcon, TRUE);
    SetIcon(m_hIcon, FALSE);

    // TODO: Add extra initialization here

 Combo = new CComboBox;
  Combo->Create(WS_CHILD | WS_VISIBLE |
  WS_VSCROLL | CBS_DROPDOWNLIST,
  CRect(10, 50, 100, 150), this, 0x1448);

  Combo->AddString("First");
  Combo->AddString("Second");
  Combo->AddString("Third");


return TRUE; // return TRUE unless you set the focus to a control
}

Can you help me further? Or give me a link with this information
First you need to create a function for the combo box change event to hold, you can do so by going to the CProjectDlg.h (corresponding header file) and then writing a function : afx_msg void changedcombo(); where changedcombo() is the name of the combobox we will be using. Then return to the corresponding .cpp file, in your case CProjectDlg.cpp and declare the following function inside BEGIN_MESSAGE_MAP(), which should be a predefined function in the class as: ON_CBN_SELCHANGE(0x1448,&CProjectDlg::OnComboChang ed). Now is the time to define the funtion, we can do so by writing our funtion as void CProjectDlg::OnComboChanged(){ } and inside the funtion we can type int nIndex = Combo->GetCurSel();
CString str;
Combo->GetLBText(
nIndex, str);
and get the current item selected. Hope this helps.