C # Listbox set selected item

I have a C # list with values

Profile 1
Profile 2
Profile 3

I want to have Profile 2 selected when loading the form. How to do it?

+5
source share
3 answers

Set the property ListBox.SelectedIndexto the Form.Shownevent.
For instance:

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;
}
+17
source

Put the following code in the event Form.Loaded:

listBox1.SelectedItem = "Profile 2";
+7
source

listBox1.Items.Add( " 1" );

listBox1.Items.Add( " 2" );

listBox1.Items.Add( " 3" );

listBox1.SelectedIndex = 1;

0

All Articles