我有一个带有值的 C# 列表框
Profile 1
Profile 2
Profile 3
我想在表单加载时选择Profile 2 。我该怎么做呢?
我有一个带有值的 C# 列表框
Profile 1
Profile 2
Profile 3
我想在表单加载时选择Profile 2 。我该怎么做呢?
在事件中设置ListBox.SelectedIndex
属性。
例如:Form.Shown
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;