Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vb.net retrieve items from Listview on SelectedIndexChanged

Posted on 2013-11-16
5
Medium Priority
?
1,671 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
ID: 39653076
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 2000 total points
ID: 39653078
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
ID: 39653089
@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
ID: 39653090
This is perfect. Great.
0
 

Author Closing Comment

by:peter-cooper
ID: 39653154
Thanks very much
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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 …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Loops Section Overview
Suggested Courses

972 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