tags:

views:

100

answers:

2

I just created a ComboBox in my VS2008. I put four items for it, One, Two, Three and Four. When I run it, nothing displayed by default. I need to select one to display. How can I make it displaying the second item by default at very beginning without my selection? I tried by put a number inside the DisplayMember and ValueMeme property but it doesn't work.

thanks,

+4  A: 

Use the SelectedIndex property:

private void MyForm_Load(object sender, EventArgs e)
{
    ComboBox1.SelectedIndex = 0;
}
Jon Seigel
there is no such property
5YrsLaterDBA
It doesn't show up in the properties list, but can be set in the code-behind.
Jon Seigel
Yes there is: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindex.aspxcomboBox1.SelectedIndex = 0; //Set selected index to first item.
Stefan
for WinForms ( follow the link ) there IS a SelectedIndex property.
Muad'Dib
or if you wanted to select the value instead -- comboBox1.SelectedValue = "SomeValue";
TGuimond
I thought I can do it at the Design mode by put an index in one of its properties.
5YrsLaterDBA
@5Yrs: All of those types of properties are hidden in the properties window. I assume it's because there would be interaction between them and the `Items` collection, causing things to change around. For example, with 4 items in the `Items` collection, if it allowed you to set `SelectedIndex` to 3, then you deleted all the items, it would have to reset `SelectedIndex` to a different value because 3 was no longer valid. That's just a guess on my part.
Jon Seigel
+1  A: 

You can set the SelectedIndex property of the combobox. Setup work like this is often done in the Form's Load event.

Adam Robinson