PlayHardGoPro
PlayHardGoPro

Reputation: 2933

Set the selected item of a listview to nothing

It used to work perfect... But now it's firing an error and I have no idea why. Here's my code:

    Private Function bListNames() As Boolean
    On Error GoTo Treat_Errors
    Dim lLinha                      As Long
    bListaNomes = False
    ListView1.ColumnHeaders.Clear
    ListView1.ListItems.Clear

    Set clmX = ListView1.ColumnHeaders.Add(1, , "Desc", 4000)
    Set clmX = ListView1.ColumnHeaders.Add(2, , "Cod", 1000, 1)
    lLinha = 1
    Do While Not rstListNames.EOF
        Set itmX = ListView1.ListItems.Add(lLinha, , "" & rstListNames.Fields(1))
            itmX.SubItems(1) = format(rstListNames.Fields(0), "000000")
        rstListNames.MoveNext
        lLinha = lLinha + 1
    Loop
    ListView1.SelectedItem.Selected = False
    bListaNomes = True
    bRetorno = True
    Exit Function
Trata_Erros:
    Exibe_Erros Err.Description, Err.Number
End Function

It fires:

<Object Variable or With Block variable not Set>

At this line:

ListView1.SelectedItem.Selected = False

Upvotes: 2

Views: 6487

Answers (1)

Alex K.
Alex K.

Reputation: 175826

That assumes that there already is a selected item in the listview which there may not be so;

if not ListView1.SelectedItem is nothing then
 ListView1.SelectedItem.Selected = False
end if

Upvotes: 2

Related Questions