Welcome to Dream.In.Code
Become a VB Expert!

Join 149,448 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,165 people online right now. Registration is fast and FREE... Join Now!




determining selection in multi seelction listbox

 
Reply to this topicStart new topic

determining selection in multi seelction listbox

rmarma
16 Nov, 2007 - 12:50 PM
Post #1

New D.I.C Head
*

Joined: 16 Nov, 2007
Posts: 1


My Contributions
sample code from intro to vb using .net (dana l.wyatt)
is as follows
********************************
CODE
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
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

also get invalid cast when try
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 = 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")
    End Sub

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Determining Selection In Multi Seelction Listbox
16 Nov, 2007 - 01:36 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Try something like this...

CODE

Dim drv As DataRowView = CType(lstAnimals.SelectedItems.Item(index), DataRowView)


This should get the individual item as an object and successfully convert it to a DataRowView. Once you have that, you should be able to get at drv. Your code where you tried something like this was using an index on the property SelectedItems which would have returned a string (the letter in the selected line, not the object itself.

See if that works for you.

smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 12:57PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month