C#Listbox设置所选项目

Ozz*_*zzy 5 c# listbox selected listboxitem winforms

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

Profile 1
Profile 2
Profile 3
Run Code Online (Sandbox Code Playgroud)

我想在表单加载时选择配置文件2.我该怎么做呢?

Don*_*nut 19

ListBox.SelectedIndexForm.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;
}
Run Code Online (Sandbox Code Playgroud)


dec*_*one 6

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

listBox1.SelectedItem = "Profile 2";
Run Code Online (Sandbox Code Playgroud)