Group,

I'm working with a ListView box for the first time. I've figured out how to populate it with data from a database. I now want to click one line of that ListView and have it return each of the five cells from that line into 5 textboxes. I've written the following code that is failing:

Private Sub lvPrinterSetup_Click(sender As Object, e As System.EventArgs) Handles lvPrinterSetup.Click
        Dim values As New List(Of String)
        If lvPrinterSetup.SelectedItems.Count > 0 Then
            For Each item As ListViewItem.ListViewSubItem In lvPrinterSetup.SelectedItems(0).SubItems
                values.Add(item.Text)
                txbRow.Text = Convert.ToString(lvPrinterSetup.SelectedItems(0))
                txbPrinterName.Text = Convert.ToString(lvPrinterSetup.SelectedItems(1))
                txbPrinterName.Text = Convert.ToString(lvPrinterSetup.SelectedItems(2))
                txbPrinterNo.Text = Convert.ToString(lvPrinterSetup.SelectedItems(3))
                txbPrinterAddress.Text = Convert.ToString(lvPrinterSetup.SelectedItems(4))
            Next

        End If
End Sub

This is failing with error notes saying the .SelectedItems(1) "InvalidArgument=Value of '1' is not valid for 'index'. Parameter name: index"

I'm sure that 2 - 4 will fail as well too. Clearly I've written this incorrectly. This is where my lack of programming knowledge and experience is showing up!!

So how do I get these 5 items from one line that I've clicked into the various textboxes?

In advance, thanks for your assistance

Don

Group,

Call off the dogs..... I've found the answer!

Private Sub lvPrinterSetup_Click(sender As Object, e As System.EventArgs) Handles lvPrinterSetup.Click

        Dim values As New List(Of String)

        If lvPrinterSetup.SelectedItems.Count > 0 Then
            txbRow.Text = lvPrinterSetup.SelectedItems(0).SubItems(0).Text
            txbPrinterName.Text = lvPrinterSetup.SelectedItems(0).SubItems(1).Text
            txbPrinterNo.Text = lvPrinterSetup.SelectedItems(0).SubItems(2).Text
            txbLoc.Text = lvPrinterSetup.SelectedItems(0).SubItems(3).Text
            txbPrinterAddress.Text = lvPrinterSetup.SelectedItems(0).SubItems(4).Text
        End If

    End Sub

It works like a champ!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.