1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to create variable for a dynamically created combo box

Discussion in 'Win32' started by stp, Feb 9, 2008.

  1. stp

    stp New Member

    Joined:
    Feb 8, 2008
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Make Combo box pointer class variable and then get the selected item from the combo box variable
     
  3. stp

    stp New Member

    Joined:
    Feb 8, 2008
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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.
     
  5. stp

    stp New Member

    Joined:
    Feb 8, 2008
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  6. hansraj

    hansraj New Member

    Joined:
    Mar 23, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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::OnComboChanged). 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.
     

Share This Page