2010-12-15 161 views
5

我有值C#列表框組選擇的項目

Profile 1 
Profile 2 
Profile 3 

我想有資料2選擇的形式的負載時,C#列表框。我該怎麼做呢?

+1

你的意思是一個Winforms列表框? C#本身可以用於許多不同的UI框架... – 2010-12-15 18:23:26

回答

17

Form.Shown事件中設置ListBox.SelectedIndex屬性。
例如:在Form.Loaded事件

public Form1() 
{ 
    InitializeComponent(); 

    // Adding the event handler in the constructor 
    this.Shown += new EventHandler(Form1_Shown); 
}  

private void Form1_Shown(object sender, EventArgs e) 
{ 
    myListBox.SelectedIndex = 1; 
} 
+0

完美謝謝 – Ozzy 2010-12-15 18:36:43

7

穿戴以下代碼:

listBox1.SelectedItem = "Profile 2"; 
0

listBox1.Items.Add( 「資料1」);

listBox1.Items.Add(「Profile 2」);

listBox1.Items.Add(「Profile 3」);

listBox1.SelectedIndex = 1;