Skip to content
Related Articles

Related Articles

Improve Article

C# | ListBox Class

  • Last Updated : 05 Sep, 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. 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

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

Properties

PropertyDescription
AutoSizeThis property is used to get or set a value that indicates whether the control resizes based on its contents.
BackColorThis property is used to get or set the background color for the control.
BorderStyleThis property indicates the border style for the control.
FontThis property is used to get or set the font of the text displayed by the control.
ForeColorThis property is used to get or set the foreground color of the control.
HeightThis property is used to get or set the height of the control.
LocationThis 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.
NameThis property is used to get or set the name of the control.
TabStopThis 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.
SizeThis property is used to get or set the height and width of the control.
TextThis property is used to get or set the text to be displayed in the RichTextBox control.
VisibleThis property is used to get or set a value indicating whether the control and all its child controls are displayed.
WidthThis property is used to get or set the width of the control.
ColumnWidthThis property is used to get or set the width of columns in a multicolumn ListBox.
HorizontalExtentThis property is used to get or set the width by which the horizontal scroll bar of a ListBox can scroll.
ItemHeightThis property is used to get or set the height of an item in the ListBox.
ItemsThis property is used to get the items of the ListBox.
PreferredHeightThis property is used to get the combined height of all items in the ListBox.
SelectedIndexThis property is used to get or set the zero-based index of the currently selected item in a ListBox.
SelectedItemThis property is used to get or set the currently selected item in the ListBox.
SelectedIndicesThis property is used to get a collection that contains the zero-based indexes of all currently selected items in the ListBox.
SortedThis property is used to get or set a value indicating whether the items in the ListBox are sorted alphabetically.
TopIndexThis property is used to get or set the index of the first visible item in the ListBox.



My Personal Notes arrow_drop_up
Recommended Articles
Page :