VB.NET Technology

VB.Net Projects

VB.Net Project 1

ListBox Controls
Previous Home Next
adplus-dvertising

These controls are used to show a list of strings from which one or more can be selected at a time. Just like check boxes and radio buttons, the list box provides a means of asking the user to make one or more selections.

You should use a list box when at design time you don't know the actual number of values the user can choose from (an example could be a list of co-workers). Even if you know all the possible values at design time, you should consider using a list box if there are a great number of values

Properties of Listbox

NameAvailabilityDescription
SelectedIndexRead/WriteThis value indicates the zero-based index of the selected item in the list box. If the list box can contain multiple selections at the same time, this property holds the index of the first item in the selected list.
ColumnWidthRead/Write In a list box with multiple columns, this property specifies the width of the columns.
ItemsRead/WriteThe Items collection contains all of the items in the list box. You use the properties of this collection to add and remove items.
MultiColumnRead/Write A list box can have more than one column. Use this property the get or set the number of columns in the list box.
SelectedIndicesRead/WriteThis property is a collection, which holds all of the zero-based indices of the selected items in the list box.
SelectedItemRead/WriteIn a list box where only one item can be selected, this property contains the selected item if any. In a list box where more than one selection can be made, it will contain the first of the selected items.
SelectedItemsRead/WriteThis property is a collection, which contains all of the items currently selected.
SelectionModeRead/Write

You can choose between four different modes of selection in a list box:

q None: No items can be selected.

q One: Only one item can be selected at any time.

q MultiSimple: Multiple items can be selected.

q MultiExtended: Multiple items can be selected and the user can use the Ctrl, Shift and arrows keys to make selections.

SortedRead/Write Setting this property to true will cause the ListBox to sort the items it contains alphabetically.
TextRead/WriteWe've seen Text properties on a number of controls, but this one works very differently than any we've seen so far. If you set the Text property of the list box control, it searches for an item that matches the text, and selects it. If you get the Text property, the value returned is the first selected item in the list. This property cannot be used if the SelectionMode is None.
CheckedIndiciesRead/Write (CheckedListBox only) This property is a collection, which contains all indexes in the CheckedListBox that is a checked or indeterminate state.
CheckedItemsRead/WriteCheckedListBox only) This is a collection of all the items in a CheckedListBox that are in a checked or indeterminate state.
CheckedItemsRead/Write(CheckedListBox only) This is a collection of all the items in a CheckedListBox that are in a checked or indeterminate state.
CheckOnClickRead/Write (CheckedListBox only) If this property is true, an item will change its state whenever the user clicks it.
ThreeDCheckBoxesRead/Write(CheckedListBox only) You can choose between Checkboxes that are flat or normal by setting this property.
AnchorRead/Writedid.
AnchorRead/Writedid.
AnchorRead/Writedid.
AnchorRead/Writedid.

ListBox Methods

ListBox Events

NameDescription
ClearSelected Clears all selections in the ListBox,
FindString Finds the first string in the ListBox beginning with a string you specify for example FindString("a") will find the first string in the ListBox beginning with 'a'
FindStringExact Like FindString but the entire string must be matched
GetSelectedReturns a value that indicates whether an item is selected
SetSelected Sets or clears the selection of an item
ToString Returns the currently selected item
GetItemChecked(CheckedListBox only) Returns a value indicating if an item is checked or not
GetItemCheckState(CheckedListBox only) Returns a value indicating the check state of an item
SetItemChecked (CheckedListBox only) Sets the item specified to achecked state.
SetItemCheckState(CheckedListBox only) Sets the check state of an item
NameDescription
ItemCheck(CheckedListBox only) Occurs when the check state of one of the list items changes
SelectedIndexChangedOccurs when the index of the selected item changes

Sample Example for ListBox control

In following image i have taken 2 ListBox controls and 4 buttons and add items in ListBox one with collection property of this.and change property according to your choice.

My purpose of taking two ListBox is for moving items from listbox1 to ListBox 2 by button click ad show below with single selection and multi selection if user click on button without selecting item from listbox1 then he/she will get alert message for choosing items from ListBox.

Code for this is given below

Write following code on Single selection Button by double click on button.

Private Sub Button1_Click_1(ByVal sender As System.Object, 
	ByVal e As System.EventArgs) Handles Button1.Click
        If listBox1.SelectedIndex >= 0 Then
            Dim selectitem As String = listBox1.SelectedItem.ToString()
            listBox2.Items.Add(selectitem)
            listBox1.Items.Remove(selectitem)
        Else
            MessageBox.Show("Please Choose Item to move", "Warning")
        End If
    End Sub

Code for moving all items from one ListBox to another.

Private Sub Button2_Click_1(ByVal sender As System.Object, 
	ByVal e As System.EventArgs) Handles Button2.Click
        For Each item As String In listBox1.Items
            listBox2.Items.Add(item)
        Next
        listBox1.Items.Clear()
    End Sub

You will get output as follows

Similarly you can write for moving items from listbox2 to listbox1.

Previous Home Next