Solved

vb.net retrieve items from Listview on SelectedIndexChanged

Posted on 2013-11-16
5
1,337 Views
Last Modified: 2013-11-16
I am trying to retrieve items from a Listview SelectedIndexChange event and am struggling to find a way to do it.

I have included my code that populates the Listview and that is working ok. All I need to do is put the results in a string 'str' which I declared elsewhere.

I would be grateful if someone could show me a way or point me in the right direction.



Sub filllistview()
        Try
            'creatconn()
            cn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand("Select * from Customers", cn)
            dr = cmd.ExecuteReader()
            While dr.Read()
                ListView1.Items.Add(dr(0).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(1))

            End While

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        Finally
            dr.Close()
            cn.Close()
        End Try
    End Sub

Open in new window

0
Comment
Question by:peter-cooper
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:Alok-Agarwal
Comment Utility
Please refer a sample code below from some other example.

' Uses the SelectedItems property to retrieve and tally the price  
' of the selected menu items.
Private Sub ListView1_SelectedIndexChanged
    (ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles ListView1.SelectedIndexChanged

    Dim breakfast As ListView.SelectedListViewItemCollection = _
        Me.ListView1.SelectedItems
    Dim item As ListViewItem
    Dim price As Double = 0.0
    For Each item In breakfast
        price += Double.Parse(item.SubItems(1).Text)
    Next

    ' Output the price to TextBox1.
    TextBox1.Text = CType(price, String)
End Sub
0
 
LVL 5

Accepted Solution

by:
Alok-Agarwal earned 500 total points
Comment Utility
so basically you will have to declare a variable of ListView.SelectedListViewItemCollection  Type and then loop through each of those using for each loop. To retrieve the selected item text you can use (item.SubItems(1).Text
0
 

Author Comment

by:peter-cooper
Comment Utility
@alok
made some minor changes and need your confirmation that although this is working, it is correct coding.

I made changes to my while statement. Is there an easier way to do this or is this correct? many thanks

While dr.Read()
                ListView1.Items.Add(dr(0).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(1))
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(2))
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(6))

            End While

Open in new window


And my for code:

Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As EventArgs) Handles ListView1.SelectedIndexChanged
        Try

            Dim test As ListView.SelectedListViewItemCollection = _
            Me.ListView1.SelectedItems
            Dim item As ListViewItem

            Dim code As String = Nothing
            Dim name As String = Nothing
            Dim address As String = Nothing
            Dim email As String = Nothing

            For Each item In test
                code += item.SubItems(0).Text
                name += item.SubItems(1).Text
                address += item.SubItems(2).Text
                email += item.SubItems(3).Text
            Next

            ' Output the price to TextBox1.
            txtname.Text = (code)
            txtfathername.Text = (name)
            txtaddress.Text = (address)
            txtemail.Text = (email)

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try
    End Sub

Open in new window

0
 
LVL 5

Expert Comment

by:Alok-Agarwal
Comment Utility
This is perfect. Great.
0
 

Author Closing Comment

by:peter-cooper
Comment Utility
Thanks very much
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now