Open In App

C# | ListBox Class

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.

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:

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:

Constructor

Constructor Description
ListBox() This Constructors is used to initialize a new instance of the ListBox class.

Properties

Property Description
AutoSize This property is used to get or set a value that indicates whether the control resizes based on its contents.
BackColor This property is used to get or set the background color for the control.
BorderStyle This property indicates the border style for the control.
Font This property is used to get or set the font of the text displayed by the control.
ForeColor This property is used to get or set the foreground color of the control.
Height This property is used to get or set the height of the control.
Location This property is used to get or set the coordinates of the upper-left corner of the ListBox control relative to the upper-left corner of its form.
Name This property is used to get or set the name of the control.
TabStop This property is used to get or set a value that shows whether the user can press the TAB key to provide the focus to the ListBox.
Size This property is used to get or set the height and width of the control.
Text This property is used to get or set the text to be displayed in the RichTextBox control.
Visible This property is used to get or set a value indicating whether the control and all its child controls are displayed.
Width This property is used to get or set the width of the control.
ColumnWidth This property is used to get or set the width of columns in a multicolumn ListBox.
HorizontalExtent This property is used to get or set the width by which the horizontal scroll bar of a ListBox can scroll.
ItemHeight This property is used to get or set the height of an item in the ListBox.
Items This property is used to get the items of the ListBox.
PreferredHeight This property is used to get the combined height of all items in the ListBox.
SelectedIndex This property is used to get or set the zero-based index of the currently selected item in a ListBox.
SelectedItem This property is used to get or set the currently selected item in the ListBox.
SelectedIndices This property is used to get a collection that contains the zero-based indexes of all currently selected items in the ListBox.
Sorted This property is used to get or set a value indicating whether the items in the ListBox are sorted alphabetically.
TopIndex This property is used to get or set the index of the first visible item in the ListBox.

Article Tags :
C#