C# Listbox set selected item

大城市里の小女人 提交于 2019-12-03 08:06:58

Set the ListBox.SelectedIndex property in the Form.Shown event.
For example:

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;
}

Put following code in Form.Loaded event:

listBox1.SelectedItem = "Profile 2";

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

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

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

listBox1.SelectedIndex = 1;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!