How to retrieve Listbox Multiple Values?

Joined
Jul 6, 2006
Messages
4
Programming Experience
3-5
Hi,
I have created one Listbox.i set the listbox SelectionMode proerty to MultiExtended.Then i assign the ListBox to Datasource the code is like this
ListBox2.ValueMember = "Empid"
ListBox2.DataSource = dsEmp.Tables("EmpTable")
ListBox2.DisplayMember = "EmpName"
Where Emp is General Table and Empid contains integer values and Empname is string.
If user select only one list then it will retrieve the value of ListBox that is
ListBox2.SelectedValue. it will retrieve correct value.
But if you user select Multiple values then it will not retrieve.
Please let me know how can i retrieve the Multiple Values here Multiple Values means Empid values.
I wishes Advance Thanks for who will replay the solution for this problem

Thanks
Ramana
 
I think you'll find that the listbox has a selected items property that is basically a collection of all the currently selected items in the listbox. You can iterate through the selected items with a for next loop. I don't want to sound harsh and it's my pleasure to help but this kind of thing is available on msdn and in the vs helpfiles as well.
 
You should itterate through the items collection of the listBox which i am not sure how the items are considered as i do not use often this control. But you can try with object anyway (maybe other helper from the forum will know better about it).
Something like this:

VB.NET:
[COLOR=blue]For each[/COLOR] lbItem [COLOR=blue]As[/COLOR] object in [COLOR=blue]Me[/COLOR].Listbox1.Selecteditems
[COLOR=teal]'get all the selected values and do something with them i guess you have an idea what to do ..right?[/COLOR]
[COLOR=blue]Next[/COLOR]
Regards ;)
 
kulrom said:
VB.NET:
[COLOR=blue]For each[/COLOR] lbItem [COLOR=blue]As[/COLOR] object in [COLOR=blue]Me[/COLOR].Listbox1.Selecteditems
[COLOR=teal]'get all the selected values and do something with them i guess you have an idea what to do ..right?[/COLOR]
[COLOR=blue]Next[/COLOR]

VB.NET:
For each LbItem as ListBoxItem in me.Listbox1.SelectedItems
 
'Do whatever
 
Next
 
I agree, listview rocks. I'd always choose it over a listbox. I found that nine times out of ten if you use a listbox then later you wished you had'nt because you tend to need some extra functionality somewhere down the line.
 
There is no such thing as ListBoxItem, the Items and SelectedItems collections contain Objects. If you know what kind of object you put into the ListBox and these are uniform objects you may do "For Each it As myclass In ListBox1.SelectedItems"... Most commonly these are strings, which resort to "For Each str As String In ListBox1.SelectedItems".
 
However, the ways are pretty same ...itterating through items collection to get all the selectedItem values.
The original threader will be satisfied with all the answers i beleive cuz if i may: say if he does try my example and found that object doesn't match the items collection with some research of internet will easyly find what he needs.

Regards ;)
 
JohnH said:
There is no such thing as ListBoxItem, the Items and SelectedItems collections contain Objects. If you know what kind of object you put into the ListBox and these are uniform objects you may do "For Each it As myclass In ListBox1.SelectedItems"... Most commonly these are strings, which resort to "For Each str As String In ListBox1.SelectedItems".


Apologies, i was thinking 'ListViewItem' Got myself a bit confused
 
vis781 said:
I agree, listview rocks. I'd always choose it over a listbox. I found that nine times out of ten if you use a listbox then later you wished you had'nt because you tend to need some extra functionality somewhere down the line.

Apart from when you come to use the SelectedItemChanged or ListIndexChanged events :)
 
hi , you can do this trick, by looping on all selected values to add them to
An array object. then clear the selections.
After the list has clearance of any selection.
Reselect the item one by one and get its selected value,
by using SetSelected method.
see the code for that trick.. on Button click event.
VB.NET:
[SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] ListBox1
[/SIZE][SIZE=2][COLOR=#008000]'Clear the arryList Items.
[/COLOR][/SIZE][SIZE=2]arrListGetIndex.Clear()
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] .Items.Count - 1
[/SIZE][SIZE=2][COLOR=#008000]'Get the Selected Indexes.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] .GetSelected(i) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]arrListGetIndex.Add(i)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] da [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] OleDb.OleDbDataAdapter
da = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] OleDbDataAdapter([/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2], cnn)
 
[/SIZE][SIZE=2][COLOR=#008000]'Check if table existed and loaded, then clear it .
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] IsNotFirstTimeLoading [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]ds.Tables([/SIZE][SIZE=2][COLOR=#800000]"childTable"[/COLOR][/SIZE][SIZE=2]).Clear()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] arrListGetIndex.Count - 1
[/SIZE][SIZE=2][COLOR=#008000]'Set the selected item depend on index.
[/COLOR][/SIZE][SIZE=2].SetSelected([/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](arrListGetIndex(i), [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2]), [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000]'Assign the query string.
[/COLOR][/SIZE][SIZE=2]da.SelectCommand.CommandText = _
[/SIZE][SIZE=2][COLOR=#800000]"SELECT Bekalh.TypeE, Asnaf.Senf1 FROM Bekalh "[/COLOR][/SIZE][SIZE=2] & _
[/SIZE][SIZE=2][COLOR=#800000]"INNER JOIN Asnaf ON Bekalh.ID = Asnaf.IDTypeE "[/COLOR][/SIZE][SIZE=2] & _
[/SIZE][SIZE=2][COLOR=#800000]"WHERE Asnaf.IDTypeE = "[/COLOR][/SIZE][SIZE=2] & .SelectedValue.ToString
 
[/SIZE][SIZE=2][COLOR=#008000]'fill data set.
[/COLOR][/SIZE][SIZE=2]da.Fill(ds, [/SIZE][SIZE=2][COLOR=#800000]"childTable"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000]'Binding datagrid.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataGrid1.DataSource = ds
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataGrid1.DataMember = [/SIZE][SIZE=2][COLOR=#800000]"childTable"
 
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Clear the Selection Items.
[/COLOR][/SIZE][SIZE=2].ClearSelected()
.SelectedIndex = -1
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Assign true after the first time button click.
[/COLOR][/SIZE][SIZE=2]IsNotFirstTimeLoading = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'If you want to keep selection on ListBox1
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Uncomment these script.
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'For i As Integer = 0 To arrListGetIndex.Count - 1
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' .SetSelected(CType(arrListGetIndex(i), Integer), True)
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' .SetSelected(CType(arrListGetIndex(i), Integer), True)
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'=========================
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'==========================
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
MessageBox.Show(ex.Message)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

I hope this can help..
 
Last edited:
Okay, so you access every item in the list's selectedItems (which is a collection, so do we really need to put them in an array?), and store them in and array, then clear the list selections, then re-select every item based on the array?

why would you do this?
 
Back
Top