Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize a value in comboBox in C#

Tags:

c#

I realize that my comboBox is always empty when program start. I have to click the arrow beside to choose a value. How do we do it so that the comboBox will show a value when the program start?

like image 287
maniac84 Avatar asked Jul 24 '12 10:07

maniac84


People also ask

How do I set default value in ComboBox?

With a combo box, “default” isn't a useful property. To set the default value, use “DefaultSelectedItems” instead. This causes specified records to display by default. To set DefaultSelectedItems, use the Filter function for the same datasource you used for your Items property.

How do I edit items in ComboBox?

Double-click an item to edit the name of the item.


2 Answers

There are the following 4 properties that you can set:

// Gets or sets the index specifying the currently selected item.
comboBox1.SelectedIndex = someIndex;  //int

// Gets or sets currently selected item in the ComboBox.
comboBox1.SelectedItem = someItem; // object

// Gets or sets the text that is selected in the editable portion of a ComboBox.
comboBox1.SelectedText = someItemText; // string

// Gets or sets the value of the member property specified by the ValueMember property. 
comboBox1.SelectedValue = someValue; // object

Comment lines straight from MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx

like image 133
Dave New Avatar answered Sep 19 '22 00:09

Dave New


If you have a combo box and want to set it's Data Source you may do it like this:

 string[] items = new string[]{"Ram","Shyam"};
    comboBox1.DataSource = items;
    comboBox1.SelectedIndex = 0;

So try to set the SelectedIndex to first index.

like image 28
Embedd_0913 Avatar answered Sep 22 '22 00:09

Embedd_0913