我有一个 C# 的列表框,其中包含一些值。
Profile 1
Profile 2
Profile 3
当表单加载时,我希望选择个人资料 2。我该怎么做?
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;