Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Listbox set selected item

i have a C# listbox with the values

Profile 1
Profile 2
Profile 3

I want to have Profile 2 selected when the form loads. How do I do this?

like image 234
Ozzy Avatar asked Dec 15 '10 18:12

Ozzy


2 Answers

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;
}
like image 72
Donut Avatar answered Sep 18 '22 17:09

Donut


Put following code in Form.Loaded event:

listBox1.SelectedItem = "Profile 2";
like image 39
decyclone Avatar answered Sep 18 '22 17:09

decyclone