0

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

1
Contributor
1
Reply
11
Views
4 Years
Discussion Span
Last Post by doncwilson_1
0

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!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.