Open In App

C# | ListBox Class

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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:

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(); 
    
  • 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);
    
  • 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:

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.


Last Updated : 05 Sep, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads