C# | ListBox Class - GeeksforGeeks

05 Sep, 2019
In Windows Forms, ListBox control is used to show multiple elements in a list, from which a user can select one or more elements and the elements are generally displayed in multiple columns. The ListBox class is used to represent the windows list box and also provide different types of properties, methods, and events. It is defined under System.Windows.Forms namespace. The ListBox class contains three different types of collection classes, i.e.
ListBox.ObjectCollection: This class holds all the elements contained in the ListBox control.
ListBox.SelectedObjectCollection: This class holds a collection of the selected items which is a subset of the items contained in the ListBox control.
ListBox.SelectedIndexCollection: This class holds a collection of the selected indexes, which is a subset of the indexes of the ListBox.ObjectCollection and these indexes specify elements that are selected.
In C# you can create a ListBox in the windows form by using two different ways:
1. Design-Time: It is the easiest way to create a ListBox as shown in the following steps:
Step 1: Create a windows form as shown in the below image:Visual Studio -> File -> New -> Project -> WindowsFormApp
Step 2: Next, drag and drop the ListBox control from the toolbox to the form.
Step 3: After drag and drop you will go to the properties of the ListBox control to modify ListBox according to your requirement.Output:
Output:
2. Run-Time: It is a little bit trickier than the above method. In this method, you can create a ListBox control programmatically with the help of syntax provided by the ListBox class. The following steps show how to set the create ListBox dynamically:
Step 1: Create a ListBox control using the ListBox() constructor is provided by the ListBox class.// Creating a ListBox control
ListBox mylist = new ListBox(); 

// Creating a ListBox control
ListBox mylist = new ListBox(); 

Step 2: After creating ListBox control, set the property of the ListBox control provided by the ListBox class.ListBox mylist = new ListBox(); 
        mylist.Location = new Point(287, 109); 
        mylist.Size = new Size(120, 95); 
        mylist.ForeColor = Color.Purple; 
        mylist.Items.Add(123); 
        mylist.Items.Add(456); 
        mylist.Items.Add(789);

ListBox mylist = new ListBox(); 
        mylist.Location = new Point(287, 109); 
        mylist.Size = new Size(120, 95); 
        mylist.ForeColor = Color.Purple; 
        mylist.Items.Add(123); 
        mylist.Items.Add(456); 
        mylist.Items.Add(789);

Step 3: And last add this ListBox control to the form using the following statement:// Adding ListBox control 
// to the form 
this.Controls.Add(mylist);
Example:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;  namespace WindowsFormsApp25 {  public partial class Form1 : Form {      public Form1()    {        InitializeComponent();    }      private void Form1_Load(object sender, EventArgs e)    {          // Creating and setting the        // properties of ListBox        ListBox mylist = new ListBox();        mylist.Location = new Point(287, 109);        mylist.Size = new Size(120, 95);        mylist.ForeColor = Color.Purple;        mylist.Items.Add(123);        mylist.Items.Add(456);        mylist.Items.Add(789);          // Adding ListBox control        // to the form        this.Controls.Add(mylist);    }}}Output:
// Adding ListBox control 
// to the form 
this.Controls.Add(mylist);

Example:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;  namespace WindowsFormsApp25 {  public partial class Form1 : Form {      public Form1()    {        InitializeComponent();    }      private void Form1_Load(object sender, EventArgs e)    {          // Creating and setting the        // properties of ListBox        ListBox mylist = new ListBox();        mylist.Location = new Point(287, 109);        mylist.Size = new Size(120, 95);        mylist.ForeColor = Color.Purple;        mylist.Items.Add(123);        mylist.Items.Add(456);        mylist.Items.Add(789);          // Adding ListBox control        // to the form        this.Controls.Add(mylist);    }}}
Output:
CSharp-Windows-Forms-Namespace
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# Dictionary with examples
C# | Delegates
C# | Method Overriding
C# | Abstract Classes
Difference between Ref and Out keywords in C#
Extension Method in C#
C# | Class and Object
C# | Constructors
C# | String.IndexOf( ) Method | Set - 1
C# | Replace() Method