ListBox control in ASP.NET using VB.NET

In this article we will learn how to use ListBox control in ASP. NET.
  • 2866

In this article we will learn how to use ListBox control in ASP. NET.

ListBox Control:

The ListBox control is used to create a single- or multi-selection drop-down list.

Properties:

Rows: The number of rows displayed in the list.

SelectionMode: Allows single or multiple selections.

Items: The collections of items in the list.

ForeColor: Color of the text within the control.

For example:

Drag the control ListBox and a Button on the form and select ListBox control property items to add the items in list.

Such as:

list1.gif
 

Figure 1.

Now, Click on the items property and add the items in the list.

list 2.gif
 

Figure 2.

Now click on the OK button. The form looks like this:

list3.gif
 

Figure 3.

Now double click on the Button control and add the following code.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        Dim num As Integer() = ListBox1.GetSelectedIndices()

        For Each i As Integer In num

            Response.Write(ListBox1.Items(i).Text)

        Next

    End Sub

GetSelectedIndices- gets the array of index values for currently selected items in the ListBox control.

Now save and run the application.

Single selection: This will shows the single selections from the list.

list4.gif
 

Figure 4.

Multiple selections: set the property selectionmode single to multiple. Such as

list5.gif
 

Figure 5.

Now run the application and select more than one items from the list.

list6.gif
 

Figure 6.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.