CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2007
    Posts
    35

    Set default value for Combo Box

    I have a combo box that have a list of values from 1 to 6. How can I set the default value to be 1?

  2. #2
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Set default value for Combo Box

    Hi,

    Use:

    Code:
    myComboBox.SetCurSel(0); //0 based index of combobox
    Regards,

    Laitinen

  3. #3
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    Re: Set default value for Combo Box

    Code:
    	CString strValue;
    	for (int i = 0; i < m_cmbValues.GetCount (); ++i)
    	{
    		m_cmbValues.GetLBText (i, strValue);
    		if (!strValue.CompareNoCase (_T("1")))
    		{		
    			m_cmbValues.SetCurSel (i);
    			break;
    		}
    
    	}
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Set default value for Combo Box

    Instead of searching for the text in the loop (as logan suggested) you could also use CComboBox::FindStringExact method

  5. #5
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Set default value for Combo Box

    Well I assumed that the values was 1,2,3,4,5,6 in that order.


    If not, do this;

    Code:
    int itemToSelect = combo.FindStringExact(0,_T("3"));
    combo.SetCurSel(itemToSelect);
    EDIT: VIctor was faster, as usual

    Regards,

    Laitinen

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Set default value for Combo Box

    Quote Originally Posted by laitinen
    ...
    EDIT: VIctor was faster, as usual
    Uhhm, i would say: "... as an exception"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured