Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get Multiple selections from a listbox?

Status
Not open for further replies.

enak

Programmer
Jul 2, 2002
412
US
I am having problems with the listbox. I need to get multiple selections from the listbox. This is the code that I am using:


Dim itm As ListBox.SelectedIndexCollection

Dim i As Integer = 0

'Figure out which classifications have been selected.

For Each itm In Me.lstClassifications.SelectedItems

sSQL = "select ID from tblClassification Where classification = """ & itm.Item(i).ToString & """"

i += 1

Next

I want to declare itm as a ListItem but it is not available. Why is this happening?

Thanks,
enak
 
Still no luck.

I can't even do the following:

sSQL = "SELECT ID, StateCode FROM tblState"
dr = cDb.getDataReader(sSQL)
If dr.HasRows Then
Me.cboManagers.DataSource = dr
Me.cboManagers.DisplayMember = "StateCode"
Me.cboManagers.ValueMember = "ID"
End If


I have to do the following to populate the combobox:

sSQL = "SELECT ID, StateCode FROM tblState"
dr = cDb.getDataReader(sSQL)
If dr.HasRows Then
While dr.Read
Me.cboState.Items.Add(dr.GetValue(1))
End While
End If
 
<I want to declare itm as a ListItem but it is not available. Why is this happening?
Because ListItem derives from System.Web.UI.WebControls, not System.Windows.Forms.ListControl, which doesn't have a ListItem object. The Items property is a ListBox.ObjectCollection object, which is a collection of references to any object you like. You might want to look at your other thread thread796-1127589, where I posted some info about this, and read
HTH

Bob
 
<which doesn't have a ListItem object
Correct that to "which doesn't use ListItem objects
 
As for this not working:
Code:
Dim itm As ListBox.SelectedIndexCollection
For Each itm In Me.lstClassifications.SelectedItems
A perusal of the link I provided above reveals that the SelectedItems property is of type ListBox.SelectedObjectCollection. SelectedIndexCollection is the type of the SelectedIndexes property, not the SelectedItems property. You don't say what error you get, but I can imagine some kind of type mismatch exception.

HTH

Bob
 
Do you have to do that? I seem to remember that's what I wound up doing, too. Why can't you use the SelectedItems collection?
 
I am not sure Listbox has selecteditems property does it? can you see it in object browser?
 
You are not given as many choices in a WinForm app as you are in asp.net.

I can not get them to match up. This should not be this complicated.
 
<I am not sure Listbox has selecteditems property does it?
Reading the link I provided answers in the affirmative.

<I can not get them to match up. This should not be this complicated.
Have you set the SelectionMode property?
 
Yes, I have set the SelectionMode property to MultipleExtended.

Nothing I do is working. I am beginning to think that it can't be done.
 
Well, if it can't be done, by extension, we should all just chuck the .Net environment and go home. What good is a tool that doesn't support multiselect list boxes???? LOL

Would you care to post your code? Maybe someone else can see something.
 
I was finally able to get the solution. I did it by going through Quick Watch in the debugger.

Here is the way that I have to do it:

For i = 0 To Me.lstMgrClassifications.SelectedItems.Count - 1
sClasses += CType(CType(CType(Me.lstMgrClassifications.SelectedItems(i), Object), System.Data.DataRowView).Row, System.Data.DataRow).ItemArray(0) & ","
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top