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

Dani AI

Generated

Nice find, — using the selected ListViewItem's SubItems is the correct approach. The original error happened because SelectedItems is the collection of selected rows, not the columns; asking for SelectedItems(1) tries to get a second selected row (which doesn't exist) and throws the index error. The per-column text lives in the SubItems collection of the selected ListViewItem, so pull values from SubItems rather than trying to index SelectedItems for columns.

For a slightly safer and more maintainable pattern, handle selection changes and map columns into an array of TextBoxes so you don't repeat lines. This example uses SelectedIndexChanged, clears the boxes when nothing is selected, and bounds-checks the SubItems collection before assigning.

Private Sub lvPrinterSetup_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lvPrinterSetup.SelectedIndexChanged
    Dim boxes() As TextBox = {txbRow, txbPrinterName, txbPrinterNo, txbLoc, txbPrinterAddress}

    If lvPrinterSetup.SelectedItems.Count = 0 Then
        For Each b In boxes : b.Clear() : Next
        Return
    End If

    Dim subItems = lvPrinterSetup.SelectedItems(0).SubItems
    For i As Integer = 0 To boxes.Length - 1
        boxes(i).Text = If(i < subItems.Count, subItems(i).Text, String.Empty)
    Next
End Sub

A few extra tips: set lvPrinterSetup.FullRowSelect = True and View = View.Details (and MultiSelect = False if you expect just one row) so clicks are clearer. Always check SubItems.Count before accessing an index because some rows may lack all columns if they were populated inconsistently. For richer scenarios, consider storing your data object in the ListViewItem.Tag and reading structured data back into controls rather than relying only on column text.

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.