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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

determining selected items in multi select listbox

Status
Not open for further replies.

rmarma

Programmer
Dec 13, 2001
5
US
sample code from intro to vb using .net (dana l.wyatt)
is as follows
********************************
Private Sub btnShowAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowAll.Click
If lstAnimals.SelectedItems.Count < 1 Then
Exit Sub
End If
Dim s As String
Dim index As Integer
s = lstAnimals.SelectedItems.Item(0)
For index = 1 To lstAnimals.SelectedItems.Count - 1
s &= ", " & lstAnimals.SelectedItems.Item(index)
Next
MessageBox.Show(s, "Selected Animals")
End Sub
***************************
and it works
but in my program it doesnt
I have to add .ToString to avoid
Cast from type 'DataRowView' to type 'String' is not valid.
but then it prints "System.Data.DataRowView" instead of the value member I need
**************************************
my code

If ListBox1.SelectedItems.Count < 1 Then
Exit Sub
End If
Dim s As String
Dim index As Integer
s = ListBox1.SelectedItems.Item(0).ToString
For index = 1 To ListBox1.SelectedItems.Count - 1
Console.WriteLine(ListBox1.SelectedItems.Item(index).ToString)
Next
MessageBox.Show(s, "Selected Animals")


any assistance would be greatly appreciated
 
I changed your code a bit... but this will show you how it should work.

Code:
    Private Sub btnShowAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowAll.Click
        Dim s As String = ""
        For index As Integer = 1 To lstAnimals.SelectedItems.Count - 1
            Dim drv As DataRowView = CType(lstAnimals.SelectedItem(index), DataRowView)
            s &= drv.Item("name or index of field") & ControlChars.CrLf
        Next
        MessageBox.Show(s, "Selected Animals")
    End Sub

Senior Software Developer
 
Oops... I thought I had changed the starting index, but I guess not. Here is the correction.

Code:
    Private Sub btnShowAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowAll.Click
        Dim s As String = ""
        For index As Integer = [red][b]0[/b][/red] To lstAnimals.SelectedItems.Count - 1
            Dim drv As DataRowView = CType(lstAnimals.SelectedItem(index), DataRowView)
            s &= drv.Item("name or index of field") & ControlChars.CrLf
        Next
        MessageBox.Show(s, "Selected Animals")
    End Sub

Senior Software Developer
 
get "Additional information: Specified cast is not valid."
when try

Dim s As String = ""
For index As Integer = 0 To lstAnimals.SelectedItems.Count - 1
Dim drv As DataRowView = CType(lstAnimals.SelectedItem(index), DataRowView)
s &= drv.Item("name or index of field") & ControlChars.CrLf
Next
MessageBox.Show(s, "Selected Animals")
 
this is what eventually worked

Dim SelectItems As DataRowView
For Each SelectItems In ListBox1.SelectedItems
Console.WriteLine("Display Member = {0}",
SelectItems.Row.ItemArray(0))
Console.WriteLine("Value Member = {0}",
SelectItems.Row.ItemArray(1))
Next
 
Did you change the "name or index of field" value in the following line before you ran this? You should have changed it to match your data structure prior to running it.


s &= drv.Item([red]"name or index of field"[/red]) & ControlChars.CrLf

Senior Software Developer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top