Hi

i have a listview which is populated with a number of 'Appointments' and when selected this fills a number of lables with information.

now on the first click it works fine and everything gets populated but when a different row is selected i get an error.

the code im using is

Dim selection As ListViewItem = lstDays.GetItemAt(e.X, e.Y)


        If (selection IsNot Nothing) Then
            lblCustID.Text = selection.SubItems(2).Text
            FillCustData(selection.SubItems(2).Text)
        Else
            MsgBox("There is no current appointment set for " & selection.SubItems(2).Text & " Would you like to make one?")
            Return
        End If

the error is appearing on the row 'lblCustID.Text = selection.SubItems(2).Text '
with the error message
' InvalidArgument=Value of '2' is not valid for 'index'. Parameter name: index '

i wondered if anyone could help

To select ListItems you can use ListView1.SelectedItems. This is a collection of Selected List Items. You can refer first selected item as ListView1.SelectedItems(0)

if ListView1.SelectedItems.Count > 0 then
Dim selection As ListViewItem = ListView1.SelectedItems(0)
  ' Your Selected code
End If

many thanks for your response

i have now amended my code to:

If lstDays.SelectedItems.Count > 0 Then

Dim selection As ListViewItem = lstDays.SelectedItems(0)

If lstDays.SelectedItems(0).SubItems(1).Text = "" Then
MsgBox("no appointment set")
Else
lbltest.Text = lstDays.SelectedItems(0).SubItems(1).Text
End If

End If

but im still getting an error when i click a row that has now value in column 2.

Because column one will always contain a value (Time) = i am checking that if column 2 is empty, then display message, else fill lable.

Hi
This may help u.
Draw a ListView Control (ListView1)

Try the Following code

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim list As ListViewItem
        list = ListView1.Items.Add("One")
        list.SubItems.Add(" Sub Item 1")
        list = ListView1.Items.Add("Two")
        list.SubItems.Add(" Sub Item 2")
        ListView1.Columns.Add("Name")
        ListView1.Columns.Add("Address", 150)
        ListView1.View = Windows.Forms.View.Details
        ListView1.FullRowSelect = True
    End Sub

    Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
        If ListView1.SelectedItems.Count > 0 Then
            Dim list As ListViewItem = ListView1.SelectedItems(0)
            MsgBox(list.Text & "  " & list.SubItems(1).Text)
        End If
    End Sub

hi

thanks for this.

my problem lies in the fact that the 'list.SubItems(1)' may be empty and i need to be able to detect this.

how do i find out if a subitem in a listview is empty?

regards

I understand the problem, Try this

Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
        If ListView1.SelectedItems.Count > 0 Then
            Dim list As ListViewItem = ListView1.SelectedItems(0)
            'Check whether subitems exists
            If list.SubItems.Count > 1 Then
                'Here also check whether they are empty
                If list.SubItems(1).Text <> "" Then
                    MsgBox(list.Text & "  " & list.SubItems(1).Text)
                Else
                    MsgBox("Incomplete")
                End If
            Else
                MsgBox("Incomplete")
            End If
        End If
    End Sub

this looks like it should do it

i give it a go and let u know how i get on

many thanks for your help

This question has already been answered. Start a new discussion instead.