ListBoxes

The ListBox demonstration program illustrates the use of a ListBox object.

A ListBox is for user input when there are a fixed number of predetermined alternatives. In this case, the user is allowed to choose one of 8 adjectives to be used in a complimentary sentence.

The 8 adjectives were entered into the Item property of the ListBox at design time using the collection editor:

When the program runs, the user selects one of the ListBox choices then clicks the "OK" button. The handler for that event uses the SelectedItem property of the ListBox in composing a complimentary sentence:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   lblOut.Text = "You are so " & cstr (lbxAdj.SelectedItem) & "!!"
End Sub
Note that the items in the ListBox are alphabetically sorted. This was done by setting its Sorted property to true. If it were false, the items would be listed in the order they were added.

In this example, we initialized the Item collection at design time. Alternatively, we could have initialized it at run time with a Form_Load event handler.


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.