Working with ListBox control in VB.NET

In this article you will learn about ListBox control and also see how you can use its default event to display index number.
  • 4213

A visual basic ListBox control displays a list of items from which you can make a selection. You can select one or more than one of the items from the ListBox. The ListBox control is based on the ListControl class which is based on the control class.

Properties of the ListBox:

  • MultiColumn:-Default value is false. You have to set it to true if you want the ListBox to display multiple columns 
  • Sorted:- Default value is false. You have to set it to true if you want the items displayed in the ListBox to be Sorted by alphabetical order. 
  • ScrollAlwaysVisible:- Default value is false. Setting it to true will display both vertical and horizontal scrollbar always. 
  • HorizontalScrollbar:-  Displays a horizontal scrollbar to the ListBox. It will work when the ListBox has multile columns.

ListBox Event:

The default event of ListBox is SelectedIndexChanged.

How you can work with ListBox
  • Simply just open a new project. 
  • Drag a ListBox, TextBox and a Button on Form and add some items to the ListBox by its item property. The form will look like below.

  • Items in a ListBox are referred by Index. When Items are added to the ListBox they assigned an index. Write the below code to display the index of an item.

       Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            TextBox1.Text = ListBox1.SelectedIndex
            'using the selected index property of the list box to select the index
  • Write the below code on Button Click to display the number of items avilable in the ListBox.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e _
        As System.EventArgs) Handles Button1.Click
            TextBox1.Text = ListBox1.Items.Count
            'counting the number of items in the ListBox with the Items.Count





More Articles

© 2020 DotNetHeaven. All rights reserved.