none
How to set a default value for a Combo Box?

    General discussion

  • I’m creating a Combobox dynamicly at run time, however, I don’t’ know how to set the default selected value for it!!? Thanks.



    Dim MyComboBox As New ComboBox
    MyComboBox.Name = “MyComboBox”
    MyComboBox.DataSource = MyDefaultView
    MyComboBox.DisplayMember = "ItemList"
    MyComboBox.ValueMember = "ValueList"

    ----This line has NO effect on MyComboBox!!! ----
    MyComboBox.SelectedValue = 6

    MyComboBox.Location = New Point(80, 60)
    Me.Controls.Add(MyComboBox)


    Thursday, June 16, 2005 5:25 PM

All replies

  • Hi.
    It looks to me like you want to say MyCombox.SelectedIndex, not SelectedValue?
    Thursday, June 16, 2005 11:48 PM
  • Just use 

    MyComboBox.SelectedIndex=XXXX
    Friday, June 17, 2005 1:06 AM
  • Use:

    MyComboBox.SelectedIndex = 6;
    Friday, June 17, 2005 1:29 AM
  • When setting SelectedValue, the value you assign must be in the ValueMember list.  If you want to select an item by index, use SelectedIndex.  I'm not sure from your code which you are trying to do but, if the ValueList column of MyDefaultView does not contain a 6 then your code will have no effect.
    Friday, June 17, 2005 5:36 AM
  • is the actual value of the item you are trying to set 6? or is that the index of it? If you know the order that the items are going into the comboBox, you can set MyComboBox.SelectedIndex = 6

    (Note: Remember to start counting from 0, not( at least in c# )) 
    Friday, June 17, 2005 5:47 AM
  • is the actual value of the item you are trying to set 6? or is that the index of it? If you know the order that the items are going into the comboBox, you can set MyComboBox.SelectedIndex = 6

    (Note: Remember to start counting from 0, not 1( at least in c# )) 
    Friday, June 17, 2005 5:48 AM
  • I don't know the number of items or index for that matter, however, I know the actual value which in this case is "6". When I set the index to a number other than -1 I get an error! Any idea? Thanks.
    Friday, June 17, 2005 12:02 PM
  • Ok, I got it. The trick is to add the selectedValue after adding the control to the Form!!!. Here is the code:


    Dim MyComboBox As New ComboBox

    MyComboBox.Name = “MyComboBox”
    MyComboBox.DataSource = MyDefaultView
    MyComboBox.DisplayMember = "ItemList"
    MyComboBox.ValueMember = "ValueList"
    MyComboBox.Location = New Point(80, 60)

    Me.Controls.Add(MyComboBox)
    MyComboBox.SelectedValue = 6

    Friday, June 17, 2005 12:13 PM
  • Here is work also (if you have 6 items, the last one is number 5)

     

    MyComboBox.SelectedIndex= 5

     

    Sunday, October 14, 2007 12:59 PM
  • that works in buttons but it will throw an exception when I put that under InitialzeComponent();
    Monday, April 02, 2012 4:25 AM
  • You can put it in form load method.
    Monday, April 02, 2012 8:50 AM
  • I tried doing it this way also and its not working. The code seems right but it still won't execute.

            private void excelsiorAutomobileCompanyForm_Load(object sender, EventArgs e)
            {
                string selectedColor;
                selectedColor = colorsComboBox.SelectedItem.ToString();
            }
    Monday, April 02, 2012 5:53 PM
  • You can use it like this i have used it may be it will work for you

    private void Form1_Load(object sender, EventArgs e)
            {
                combobox1.SelectedIndex = 5;
            }

    It will show the 6th item.

    Friday, April 06, 2012 9:52 AM
  • consider you've added "test1,test2 and test3" items in combobox1 so if you want to set test2 as default , you can use events and enter this code into your form :

    private void  btnEnter_click(object sender, EventArgs e)

    combobox1.Text="test2";

    "shown" event occurs whenever the form first shown.


    • Edited by samandari Thursday, September 13, 2012 9:51 AM
    Thursday, September 13, 2012 9:49 AM
  • Thank you it's works

    Ravin Singh D

    Saturday, April 27, 2013 8:11 AM
  • It works for MyComboBox.SelectedIndex = 6; Thanks.
    Saturday, June 22, 2013 2:06 PM