Open In App

How to add Items in ListBox in C#?

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:



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:


Article Tags :
C#