How to add Items in ListBox in C#? - GeeksforGeeks

11 Jul, 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. In ListBox, you can add items in the ListBox using Items Property. This property allows you to get a reference to the list of elements that are currently stored in the ListBox. With the help of this reference, you can add, remove, and get a count of the elements in the collection. You can set this property in two different ways:
1. Design-Time: It is the easiest way to add elements in the 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: Drag the ListBox control from the ToolBox and drop it on the windows form. You are allowed to place a ListBox control anywhere on the windows form according to your need.
Step 3: After drag and drop you will go to the properties of the ListBox control to add elements in the ListBox.Output:
Output:
2. RunTime: It is a little bit trickier than the above method. In this method, you can add the elements in the ListBox control programmatically with the help of given syntax:
public System.Windows.Forms.ListBox.ObjectCollection Items { get; }
Here, ListBox.ObjectCollection indicates the elements in the ListBox. The following steps show how to add elements in the ListBox dynamically:
Step 1: Create a list box using the ListBox() constructor is provided by the ListBox class.// Creating ListBox using ListBox class constructor
ListBox mylist = new ListBox();

// Creating ListBox using ListBox class constructor
ListBox mylist = new ListBox();

Step 2: After creating ListBox, set the Items property of the ListBox provided by the ListBox class.// Adding the elements in the ListBox
mylist.Items.Add("GeeksForGeeks");

// Adding the elements in the ListBox
mylist.Items.Add("GeeksForGeeks");

Step 3: And last add this ListBox control to the form using Add() method.// Add this ListBox 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 WindowsFormsApp26 {  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.BorderStyle = BorderStyle.Fixed3D;        mylist.Items.Add("Geeks");        mylist.Items.Add("GFG");        mylist.Items.Add("GeeksForGeeks");        mylist.Items.Add("gfg");          // Adding ListBox control to the form        this.Controls.Add(mylist);    }}}Output:
// Add this ListBox 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 WindowsFormsApp26 {  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.BorderStyle = BorderStyle.Fixed3D;        mylist.Items.Add("Geeks");        mylist.Items.Add("GFG");        mylist.Items.Add("GeeksForGeeks");        mylist.Items.Add("gfg");          // Adding ListBox control to the form        this.Controls.Add(mylist);    }}}
Output:
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#
C# | Class and Object
Extension Method in C#
C# | Constructors
C# | String.IndexOf( ) Method | Set - 1
C# | Replace() Method