winforms Basic controls ListBox

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Listbox is a control that can contains collection of objects. Listbox is similar to Combobox but in Combobox; Only selected items are visible to user. In Listbox; all items are visible to user.

How to add items to ListBox?

private void Form3_Load(object sender, EventArgs e)
        {
            string test = "Trial";
            string test2 = "45";
            int numberTest = 43;
            decimal decimalTest = 130;
            
            listBox1.Items.Add(test);
            listBox1.Items.Add(test2);
            listBox1.Items.Add(numberTest);
            listBox1.Items.Add(decimalTest);
        }

Output;

Result

Or datasources can be given,

private void Form3_Load(object sender, EventArgs e)
        {
            List<string> TestList = new List<string> { "test1", "test2", "test3", "44", "55" };
            listBox1.DataSource = TestList;
        }

Output;

Result2

private void Form3_Load(object sender, EventArgs e)
        {
            SqlConnection Connection = new SqlConnection("Server=serverName;Database=db;Trusted_Connection=True;"); //Connetion to MS-SQL(RDBMS)
            Connection.Open(); //Connection open
            SqlDataAdapter Adapter = new SqlDataAdapter("Select * From TestTable", Connection); // Get all records from TestTable.
            DataTable DT = new DataTable();
            Adapter.Fill(DT); // Fill records to DataTable.
            listBox1.DataSource = DT; // DataTable is the datasource.
            listBox1.ValueMember = "TestID"; 
            listBox1.DisplayMember= "TestName";
        }

The proper output;

enter image description here

Giving an external sql datasource to listbox requires, ValueMember and DisplayMember

If NOT it will looks like this,

enter image description here

Useful events;

SelectedIndex_Changed;

Define a list to give datasource

private void Form3_Load(object sender, EventArgs e)
        {
            List<string> DataList = new List<string> {"test1" , "test2" , "test3" , "44" , "45" };
            listBox1.DataSource = TestList;
        }

At the form's design select Listbox and press F4 or at right side click on lightining icon.

enter image description here

Visual studio will generate listBox1_SelectedIndexChanged to codebehind.

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int Index = listBox1.SelectedIndex;
            label1.Text = Index.ToString();
        }

Result of SelectedIndex_Changed; (label at the bottom will show the index of each selected item)

enter image description here

SelectedValue_Changed; (The datasource is same as at the top and you can generate this event like SelectedIndex_Changed)

private void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            label1.Text = listBox1.SelectedValue.ToString();
        }

Output;

enter image description here



Got any winforms Question?