Link to home
Start Free TrialLog in
Avatar of lightstream
lightstream

asked on

Validation - Listview (lvwreport)

Got a listview(lvwreport)showing db data(items). Able to highlight the desired items(selecteditems)then click on the command button(delete)to performed an erase operation.

So, how can i've validation in the command button by prompting an error msg. if there isn't any selection(highlight) in the listview ??
Avatar of AzraSound
AzraSound
Flag of United States of America image

maybe you can loop through the listitems collection:

Private Sub cmdDelete_Click()
    Dim i As Integer
    For i = 1 To ListView1.ListItems.Count
        If ListView1.ListItems(i).Selected = True Then
            ListView1.ListItems.Remove (i)
        End If
        i = i + 1
    Next
End Sub
maybe you can loop through the listitems collection:

Private Sub cmdDelete_Click()
    Dim i As Integer
    For i = 1 To ListView1.ListItems.Count
        If ListView1.ListItems(i).Selected = True Then
            ListView1.ListItems.Remove (i)
        End If
        i = i + 1
    Next
End Sub
ASKER CERTIFIED SOLUTION
Avatar of morpho
morpho

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of morpho
morpho

AzraSound - I have a nasty feeling that your code for removing elements will probably fall because your loop may go from 1 to 10 items, but after deleting say item 3, there will be no item 10.

Also... isnt the question how to detect if there is no selection, rather than how to delete items from the listview ?
you may be right, i wasnt on a machine with vb to try it out.  however as far as me not answering his question, the way he made it sound, he was getting an error when he tried to delete when nothing was selected.  why else would he want to put an error handler in there? so if my code had worked it would've killed two birds with one stone so to speak.
AzraSound -

Maybe you're right - I wasnt accusing you of not answering the question - I was just trying to clarify it.

I read it as saying "how do I see if no item is selected so I can warn the user if they hit delete" - I dont think lightstream really means error box -  more a validation message. But I may be wrong.
Set the listview's HideSelection property to False.

Private Sub ListView1_Click()
    If ListView1.SelectedItem Is Nothing Then
        cmdAllowDelete.Enabled = False
    Else
        cmdDelete.Enabled = True
    End If
End Sub

Private Sub ListView1_KeyDown(KeyCode As Integer, Shift As Integer)
    If ListView1.SelectedItem Is Nothing Then
        cmdDelete.Enabled = False
    Else
        cmdDelete.Enabled = True
    End If
End Sub

Private Sub cmdDelete_Click()
    '
    'delete the list items
    '
    cmdDelete.Enabled = False
End Sub

Avatar of lightstream

ASKER

Guys...

Sorry..about the misleading words. TQ anyway.