Hi. In this tutorial I will show you how to get the selected values in ListBox control in VB.NET. Many VB.NET developers might wonder why there's no (SelectedValues) property for ListBox control while there's (SelectedItems) property. Same I ask. Well, (SelectedValues) property is not provided in VB.NET assuming that you can get the selected values in the ListBox control from (SelectedItems) property. Using the (SelectedItems) property to get the selected values is not a clear approach.
First, even if you use (SelectedItems) property, you can get access to the values of the selected items, but it's not trivial as there's no (Value) property for a ListBox item (e.g. ListBox.SelectedItems(index).Value)
The funny part is that there's (SelectedValue) property for ListBox control that gives you the value of the first selected item, but what if (SelectionMode) property is set to MultiSimple for the ListBox control, and we want to know all the selected values in the ListBox control.
One more point is that you will not care about the selected values in the ListBox control unless you have bind the Listbox control with a member in a DataSource.
After all this discussion, I will show you how to create a generic function that takes a target ListBox control and returns an Object array that represents the selected values in the ListBox control.
I will demonstrate this tutorial by using MS Visual Studio 2010.
The (New Project) dialog displays. Click on OK, then the the form will be displayed to you in design mode.
Now, I will change SelectionMode property of lstOriginal to MultiSimple, so I can select multiple items from lstOriginal ListBox.
The following image shows that I bound lstOriginal with the previous Department table. As shown, Display Member is chosen to be the name of the department, and the Value Member is chosen to be the id of the department. You can display this (ListBox Tasks) dialog by expanding the small arrow on the top of the ListBox control.
The function header would take a reference to the target ListBox control (TargetListBox), and returns an array of objects that represent the selected values in the passed ListBox control.
We need to define three variables that will help us in doing the function's work.
After we defined the previous three variables, now we start with the part that gets the selected values in TargetListBox, There are two loops that will do this job. The first loop is responsible for copying selected indices in TargetListBox to SelectedIndiciesCopy, add the first selected value in TargetListBox to SelectedValues array, then deselect the first selected item in TargetListBox. This loop will iterate as long as TargetListBox has selected items. It will look like the following code:
The second loop is responsible for re-selecting the indices again in TargetListbox after we deselected them in the first loop. The final statement inside the function returns the Object array (SelectedValues) that contains the selected values in TargetListBox.
At this point, we completed GetSelectedValues function which returns an Object array that represents the selected values in TargetListBox. The next following code represents the event handler of the button that will call GetSelectedValues function, then updates lstSelectedValues listbox with the selected values.
The following are some screenshots that of the application:
At this point I have reached the end of this tutorial. The main part of this tutorial was about creating a generic fnction that returns the selected values in a ListBox control because VB.NET does include SelectedValues property for a ListBox control.
I hope that I helped some, See you in more tutorials.
First, even if you use (SelectedItems) property, you can get access to the values of the selected items, but it's not trivial as there's no (Value) property for a ListBox item (e.g. ListBox.SelectedItems(index).Value)
The funny part is that there's (SelectedValue) property for ListBox control that gives you the value of the first selected item, but what if (SelectionMode) property is set to MultiSimple for the ListBox control, and we want to know all the selected values in the ListBox control.
One more point is that you will not care about the selected values in the ListBox control unless you have bind the Listbox control with a member in a DataSource.
After all this discussion, I will show you how to create a generic function that takes a target ListBox control and returns an Object array that represents the selected values in the ListBox control.
I will demonstrate this tutorial by using MS Visual Studio 2010.
Creating New Windows Application in MS Visual Studio 2010
The (New Project) dialog displays. Click on OK, then the the form will be displayed to you in design mode.
Populating Application Form with Controls
I populated the form with two labels, two ListBox controls, and one button. lstOriginal contains the items with their values. lstSelectedValues will be populated with the selected values in lstOriginal after the user clicks the (Get Selected Values) button.Now, I will change SelectionMode property of lstOriginal to MultiSimple, so I can select multiple items from lstOriginal ListBox.
Binding lstOriginal with Database Table
Now, I will configure lstOriginal, and bind it with a Department table that I created using MS SQL Server 2008. Simply, the table contains three records as shown below.- id: the id of the department
- name: the name of the department
- manager: the manager id of the department
The following image shows that I bound lstOriginal with the previous Department table. As shown, Display Member is chosen to be the name of the department, and the Value Member is chosen to be the id of the department. You can display this (ListBox Tasks) dialog by expanding the small arrow on the top of the ListBox control.
First Run for the Application
Now, if we run the application in Debug mode by pressing F5 key, we will see the following screenshot, and you will notice that lstOriginal is populated with departments names that are stored in the department table shown previously.Creating Generic Function to Return lstOriginal Selected Values
Now is the time to start coding the generic function that will take not just lstOriginal, but any ListBox control and return an Object array that represents the selected values, and the caller is responsible for dealing with the returned array.The function header would take a reference to the target ListBox control (TargetListBox), and returns an array of objects that represent the selected values in the passed ListBox control.
We need to define three variables that will help us in doing the function's work.
- First, the variable (NumberOfSelectedItems) will contain the number of selected items in the TargetListBox
- Second, an integer array (SelectedIndiciesCopy) that contains a copy of selected indices in TargetListBox
- Third, an object array (SelectedValues) that will contain the selected values in TargetListBox
Private Function GetSelectedValues(ByRef TargetListBox As ListBox) As Object()
Dim NumberOfSelectedItems = TargetListBox.SelectedItems.Count
Dim SelectedIndiciesCopy(NumberOfSelectedItems - 1) As Integer
Dim SelectedValues(NumberOfSelectedItems - 1) As Object
End Function
Dim NumberOfSelectedItems = TargetListBox.SelectedItems.Count
Dim SelectedIndiciesCopy(NumberOfSelectedItems - 1) As Integer
Dim SelectedValues(NumberOfSelectedItems - 1) As Object
End Function
After we defined the previous three variables, now we start with the part that gets the selected values in TargetListBox, There are two loops that will do this job. The first loop is responsible for copying selected indices in TargetListBox to SelectedIndiciesCopy, add the first selected value in TargetListBox to SelectedValues array, then deselect the first selected item in TargetListBox. This loop will iterate as long as TargetListBox has selected items. It will look like the following code:
Private Function GetSelectedValues(ByRef TargetListBox As ListBox) As Object()
Dim NumberOfSelectedItems = TargetListBox.SelectedItems.Count
Dim SelectedIndiciesCopy(NumberOfSelectedItems - 1) As Integer
Dim SelectedValues(NumberOfSelectedItems - 1) As Object
For index As Integer = 0 To NumberOfSelectedItems - 1
SelectedIndiciesCopy(index) = TargetListBox.SelectedIndices(0)
SelectedValues(index) = TargetListBox.SelectedValue
TargetListBox.SetSelected(SelectedIndiciesCopy(index), False)
Next
End Function
Dim NumberOfSelectedItems = TargetListBox.SelectedItems.Count
Dim SelectedIndiciesCopy(NumberOfSelectedItems - 1) As Integer
Dim SelectedValues(NumberOfSelectedItems - 1) As Object
For index As Integer = 0 To NumberOfSelectedItems - 1
SelectedIndiciesCopy(index) = TargetListBox.SelectedIndices(0)
SelectedValues(index) = TargetListBox.SelectedValue
TargetListBox.SetSelected(SelectedIndiciesCopy(index), False)
Next
End Function
The second loop is responsible for re-selecting the indices again in TargetListbox after we deselected them in the first loop. The final statement inside the function returns the Object array (SelectedValues) that contains the selected values in TargetListBox.
Private Function GetSelectedValues(ByRef TargetListBox As ListBox) As Object()
Dim NumberOfSelectedItems = TargetListBox.SelectedItems.Count
Dim SelectedIndiciesCopy(NumberOfSelectedItems - 1) As Integer
Dim SelectedValues(NumberOfSelectedItems - 1) As Object
For index As Integer = 0 To NumberOfSelectedItems - 1
SelectedIndiciesCopy(index) = TargetListBox.SelectedIndices(0)
SelectedValues(index) = TargetListBox.SelectedValue
TargetListBox.SetSelected(SelectedIndiciesCopy(index), False)
Next
For index As Integer = 0 To NumberOfSelectedItems - 1
TargetListBox.SetSelected(SelectedIndiciesCopy(index), True)
Next
Return SelectedValues
End Function
Dim NumberOfSelectedItems = TargetListBox.SelectedItems.Count
Dim SelectedIndiciesCopy(NumberOfSelectedItems - 1) As Integer
Dim SelectedValues(NumberOfSelectedItems - 1) As Object
For index As Integer = 0 To NumberOfSelectedItems - 1
SelectedIndiciesCopy(index) = TargetListBox.SelectedIndices(0)
SelectedValues(index) = TargetListBox.SelectedValue
TargetListBox.SetSelected(SelectedIndiciesCopy(index), False)
Next
For index As Integer = 0 To NumberOfSelectedItems - 1
TargetListBox.SetSelected(SelectedIndiciesCopy(index), True)
Next
Return SelectedValues
End Function
At this point, we completed GetSelectedValues function which returns an Object array that represents the selected values in TargetListBox. The next following code represents the event handler of the button that will call GetSelectedValues function, then updates lstSelectedValues listbox with the selected values.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
lstSelectedValues.Items.Clear()
For Each value As Object In GetSelectedValues(lstOriginal)
lstSelectedValues.Items.Add(value)
Next
End Sub
lstSelectedValues.Items.Clear()
For Each value As Object In GetSelectedValues(lstOriginal)
lstSelectedValues.Items.Add(value)
Next
End Sub
The following are some screenshots that of the application:
At this point I have reached the end of this tutorial. The main part of this tutorial was about creating a generic fnction that returns the selected values in a ListBox control because VB.NET does include SelectedValues property for a ListBox control.
I hope that I helped some, See you in more tutorials.
No comments:
Post a Comment