ListBox in C Sharp Updated
ListBox in C Sharp Updated
ListBox in C Sharp Updated
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.
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 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:
Code for Add Item on button Click
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
Code for Multiple Selected Items Move ( Selected Move→) on button Click
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.
Step 2: After creating ListBox control, set the property of the ListBox control provided by the ListBox
class.
Step 3: And last add this ListBox control to the form using the following statement:
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 Form1()
{
InitializeComponent();
}
Output:
Constructor
Constructor Description
ListBox() This Constructors is used to initialize a new instance of the ListBox class.
Properties
Property Description
This property is used to get or set a value that indicates whether the control resizes
AutoSize
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.
This property is used to get or set the coordinates of the upper-left corner of the
Location
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.
This property is used to get or set a value that shows whether the user can press the
TabStop
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.
This property is used to get or set a value indicating whether the control and all its
Visible
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.
This property is used to get or set the width by which the horizontal scroll bar of a
HorizontalExtent
ListBox can scroll.
ItemHeight This property is used to get or set the height of an item in the ListBox.
PreferredHeight This property is used to get the combined height of all items in the ListBox.
This property is used to get or set the zero-based index of the currently selected item
SelectedIndex
in a ListBox.
SelectedItem This property is used to get or set the currently selected item in the ListBox.
This property is used to get a collection that contains the zero-based indexes of all
SelectedIndices
currently selected items in the ListBox.
This property is used to get or set a value indicating whether the items in the ListBox
Sorted
are sorted alphabetically.
TopIndex This property is used to get or set the index of the first visible item in the ListBox.