2011-09-23 90 views
0

如果我想在組合框出現時加載默認國家(「國家:黎巴嫩」),我該怎麼做? 我正在使用VS2008(C#)。謝謝。組合框中的默認加載

+1

的WinForms?的WebForms? WPF?你有什麼嘗試?你能發佈你的代碼嗎? – Oded

+0

@Oded Winforms在這種情況下。 – Adrian

回答

0

一旦載入表單,您通常只需設置myCombo.SelectedValue = "Whatever value"

0

使用SelectedValue之一,SelectedIndexSelectedItemSelectedText或屬性ComboBox控制的。

0

你可以試試這個:

myCombo.SelectedIndex = lebanonsIndex; 
0

您可以設置在窗體的Load事件選擇指數

如果黎巴嫩是第一個在comboboxItem selectedIndex = 0; 例子:

private void Form1_Load(object sender, EventArgs e) 
{ 
    comboBox1.SelectedIndex = 0; 
} 
0

您可以通過在Items集合

comboBox1.SelectedIndex = comboBox1.Items.IndexOf("Lebanon");// case sensitive 
0

這可能是工作用的IndexOf設置。將這些代碼複製到appsettings中的app.config文件中。

< add key="DefaultCountry" value="Lebanon" /> 

並將其複製到你想查看它的地方。

combobox1.Text = System.Configuration.ConfigurationManager.AppSettings["DefaultCountry"].ToString();.<add key="DefaultCountry" value="Lebanon"/> 
1

將一些項目添加到Combobox中......它只是一個示例,您可以運行循環來添加項目。

combobox1.Items.Add("USA"); 
combobox1.Items.Add("England"); 
combobox1.Items.Add("Kenya"); 
combobox1.Items.Add("South Africa"); 

現在你的Combobox有四個項目。要選擇特定的國家嘗試這個辦法:

combobox1.Text = "USA"; 

到索引基礎上選擇一個,試試這個:

combobox1.SelectedIndex = 0; // For USA 

希望它能幫助。

1

您可以使用另一種方法在組合框添加項目:

comboBox1.Items.Insert(0, "Egypt"); 
//the first item is the item index and the second is the item object.