C#Listbox设置所选项目
我有一个带有值的C#列表框
Profile 1 Profile 2 Profile 3
我想在表单加载时选择配置文件2 。 我该怎么做呢?
在Form.Shown
事件中设置ListBox.SelectedIndex
属性。
例如:
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; }
将以下代码放在Form.Loaded
事件中:
listBox1.SelectedItem = "Profile 2";
listBox1.Items.Add(“Profile 1”);
listBox1.Items.Add(“Profile 2”);
listBox1.Items.Add(“Profile 3”);
listBox1.SelectedIndex = 1;