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?
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.
Double-click an item to edit the name of the item.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With