|
-
Apr 4th, 2007, 11:33 AM
#1
[RESOLVED] [2005] Get SeletedValues from ListBox with Multi-Select
Ok, so I populate a listbox with a dataset like so
vb Code:
lbNucs.DataSource = ds.Tables(0)
lbNucs.DisplayMember = "Analyte"
lbnucs.ValueMember = "AnalytePK"
This works fine, but... I am trying to return the values of multiple selected items. It seems to only return the value of the first selected item in the group. Is it possible to do this by this method?
I have tried setting the SelectedIndex and it still only returns the value of the first item. any help would be appreciated.
Last edited by bmahler; Apr 4th, 2007 at 11:37 AM.
-
Apr 4th, 2007, 11:53 AM
#2
Re: [2005] Get SeletedValues from ListBox with Multi-Select
See SelectedIndexCollection
-
Apr 4th, 2007, 11:57 AM
#3
Re: [2005] Get SeletedValues from ListBox with Multi-Select
That only returns the indexes that are selected, I need to get the value of each selected index. I came up with a way to do it, but it seems like a real work around and there must be a better way to accomplish this.
vb Code:
Dim intNucPk As Integer
'A list to hold the indexes
Dim ids As New List(Of Integer)
'Add the selected indexes to the list
For i As Integer = 0 To Me.lbNucs.SelectedIndices.Count - 1
ids.Add(Me.lbNucs.SelectedIndices(i))
Next
'Set the selected Item to nothing
lbNucs.SelectedItem = Nothing
'Loop through the selected indicies
For i As Integer = 0 To ids.Count - 1
'Set the selected index
lbNucs.SelectedIndex = ids.Item(i)
'Get the value
intNucPk = lbNucs.SelectedValue
'Add the nuclide to the analysis code
tryAddNuc(intNucPk)
'Set the selected item to nothing
lbNucs.SelectedItem = Nothing
Next
'Repopulate the listbox with the new values
getNucsForAC()
-
Apr 4th, 2007, 12:07 PM
#4
Re: [2005] Get SeletedValues from ListBox with Multi-Select
.SelectedItem should be a collection with a zero-based index. Did you try looping through .SelectedItem(index).Value?
-
Apr 4th, 2007, 12:31 PM
#5
Re: [2005] Get SeletedValues from ListBox with Multi-Select
SelectedItems might be what you're looking for. It still requires a loop though
vb Code:
For Each SelectedItem As String In ListBox1.SelectedItems
ListBox2.Items.Add(SelectedItem)
Next
Of course, the "As String" would need to be whatever object you were storing in the listbox.
It's too bad the following doesn't work
vb Code:
ListBox2.Items.AddRange(ListBox1.SelectedItems)
But for some reason the MS .NET Team decided to make ListBox.ObjectCollection and ListBox.SelectedObjectCollection incompatible.
-
Apr 4th, 2007, 01:10 PM
#6
Re: [2005] Get SeletedValues from ListBox with Multi-Select
.SelectedItem(index).Value doe snot work. There is no value property for that.
@bgmacaw I guess you did not read my original post. I am looking to return the valuemember of each selected Item, not the index. The index is easy, but I need the ValueMember of that index. I have actually done this, by deselecting each item after it is added like so.
vb Code:
Dim intNucPk As Integer
'A list to hold the indexes
Dim ids As New List(Of Integer)
'Add the selected indexes to the list
For i As Integer = 0 To Me.lbNucs.SelectedIndices.Count - 1
ids.Add(Me.lbNucs.SelectedIndices(i))
Next
'Loop through the selected indicies
For i As Integer = 0 To ids.Count - 1
intNucPk = lbNucs.SelectedValue
'Add the nuclide to the analysis code
tryAddNuc(intNucPk)
'Deselect the item
lbNucs.SetSelected(ids(i), False)
Next
'Repopulate the listbox with the new values
getNucsForAC()
Seems like the only way to get the SelectedValue of the next item.
anyway, marking this thread resolved, because I found a solution.
-
Apr 4th, 2007, 01:47 PM
#7
Re: [2005] Get SeletedValues from ListBox with Multi-Select
 Originally Posted by bmahler
@bgmacaw I guess you did not read my original post. I am looking to return the valuemember of each selected Item, not the index. The index is easy, but I need the ValueMember of that index.
Sorry I missed that when I read your post. I didn't catch on that you meant ValueMember when you said value. I'm glad you resolved it.
Here's another way I found to do it:
vb Code:
For Each SelectedItem As DataRowView In ListBox1.SelectedItems
ListBox2.Items.Add(SelectedItem("AnalytePK"))
Next
When you databind the control it puts a DataRowView object in the Listbox's Items collection so you can access any members of a row in the original DataTable that way.
-
Apr 4th, 2007, 01:57 PM
#8
Re: [RESOLVED] [2005] Get SeletedValues from ListBox with Multi-Select
Thanks for that one, that is definitely more efficient than my method
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|