Reputation: 421
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?
Upvotes: 6
Views: 26501
Reputation: 9
If you are using WPF with MVVM and INotifyPropertiesChanged
interface, then you can set a fallback value in your binding property.
SelectedIndex="{Binding SelectedCountry.MultipleClassCountry, FallbackValue= 0}"
Setting SelectedIndex = 0
should select the first Item you have in your combobox.
Upvotes: 0
Reputation: 81
Davenewza's answer is great for the programmatic approach (Which I highly recommend). Another way that is less elegant but utilizes the properties toolbox is to do the following:
To be safe, I would enter a value that corresponds to something that will be selected in the box to prevent unwanted strings from being propagated to other functions/variables. This reason can cause a lot of headaches if not carefully handled, which is why the programmatic approach is preferred.
Upvotes: 0
Reputation: 16555
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.
Upvotes: 5
Reputation: 40002
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
Upvotes: 7