Default load in combobox
If I want to load for example a default country when the combobox shows up ("Country: Lebanon"), how do I do so? I am using VS2008 (C#). Thank yo开发者_开发技巧u.
Add some items to the Combobox...it is just a sample, you can run a loop also to add items.
combobox1.Items.Add("USA");
combobox1.Items.Add("England");
combobox1.Items.Add("Kenya");
combobox1.Items.Add("South Africa");
Now your Combobox has four items. To select a specific country try this:
combobox1.Text = "USA";
To select a on the index basis, try this:
combobox1.SelectedIndex = 0; // For USA
Hope it helps.
You can use another method to add items in comboBox:
comboBox1.Items.Insert(0, "Egypt");
//the first item is the item index and the second is the item object.
You would usually just set myCombo.SelectedValue = "Whatever value"
as soon as the form is loaded.
Use one of SelectedValue
, SelectedIndex
, SelectedItem
or SelectedText
properties of the ComboBox
control.
You could try this:
myCombo.SelectedIndex = lebanonsIndex;
You can set it by using IndexOf in the Items collection
comboBox1.SelectedIndex = comboBox1.Items.IndexOf("Lebanon");// case sensitive
This could work. Copy this code in your app.config file within appsettings.
< add key="DefaultCountry" value="Lebanon" />
and copy this at ur win form where you want to view it.
combobox1.Text = System.Configuration.ConfigurationManager.AppSettings["DefaultCountry"].ToString();.<add key="DefaultCountry" value="Lebanon"/>
You can set selected index in Form load event
If Lebanon is first in comboboxItem selectedIndex = 0
;
Example:
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 0;
}
精彩评论