6

我有一个带有值的 C# 列表框

Profile 1
Profile 2
Profile 3

我想在表单加载时选择Profile 2 。我该怎么做呢?

4

3 回答 3

22

在事件中设置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;
}
于 2010-12-15T18:22:33.197 回答
6

将以下代码放入Form.Loaded事件中:

listBox1.SelectedItem = "Profile 2";
于 2010-12-15T18:22:53.610 回答
0
listBox1.Items.Add("Profile 1");

listBox1.Items.Add("Profile 2");

listBox1.Items.Add("Profile 3");

listBox1.SelectedIndex = 1;
于 2010-12-15T18:51:27.057 回答