Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Win32 (http://www.go4expert.com/forums/win32/)
-   -   How to create variable for a dynamically created combo box (http://www.go4expert.com/forums/create-variable-dynamically-created-t8737/)

stp 9Feb2008 20:31

How to create variable for a dynamically created combo box
 
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 10Feb2008 09:39

Re: How to create variable for a dynamically created combo box
 
Make Combo box pointer class variable and then get the selected item from the combo box variable

stp 10Feb2008 14:27

Re: How to create variable for a dynamically created combo box
 
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 10Feb2008 15:36

Re: How to create variable for a dynamically created combo box
 
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 11Feb2008 00:52

Re: How to create variable for a dynamically created combo box
 
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 3Apr2010 11:20

Re: How to create variable for a dynamically created combo box
 
Quote:

Originally Posted by stp (Post 23977)
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.


All times are GMT +5.5. The time now is 16:54.