locked
highlight selected item in a listview RRS feed

  • Question

  • Hi, I'm working on a listview, I have a button which delete the selected item, the problem is that when I delete an item, internally the upper item is selected, but this selection is not visible, I want to do is that when a  item is removed the element that get the selection will be marked in blue color, this effect occurs when you delete a item of a listbox, I hope I can help me, greetings
    Wednesday, October 22, 2014 10:00 PM

Answers

  • The following is one of my examples of deleting selected rows from ListView and then highlighting the row above the deleted row. Hope it helps.

    Private Sub CommandButton1_Click()
        
        Dim response As Integer
        Dim lngIdx As Long
        
        response = MsgBox("This item will be permanently deleted. Continue?", vbYesNo + vbDefaultButton2, "Delete")
        
        If response = vbNo Then
            Exit Sub
        Else
            If Not ListView1.SelectedItem Is Nothing Then
                lngIdx = ListView1.SelectedItem.Index
                ListView1.ListItems.Remove (ListView1.SelectedItem.Index)
            ElseIf ListView1.ListItems.Count = 0 Then
                MsgBox "Nothing to delete...", vbExclamation, "Delete"
            Else
                MsgBox "Please highlight the item to delete...", vbInformation, "Delete"
            End If
        End If
       
        ListView1.SetFocus  'Must have focus to display selected as highlighted
        ListView1.ListItems(lngIdx).Selected = True
        ListView1.ListItems(lngIdx).EnsureVisible   'Will display item if scroll has hidden
        
    End Sub


    Regards, OssieMac

    • Marked as answer by aslf010990 Monday, November 3, 2014 3:11 PM
    Sunday, November 2, 2014 10:45 AM

All replies

  • The following is one of my examples of deleting selected rows from ListView and then highlighting the row above the deleted row. Hope it helps.

    Private Sub CommandButton1_Click()
        
        Dim response As Integer
        Dim lngIdx As Long
        
        response = MsgBox("This item will be permanently deleted. Continue?", vbYesNo + vbDefaultButton2, "Delete")
        
        If response = vbNo Then
            Exit Sub
        Else
            If Not ListView1.SelectedItem Is Nothing Then
                lngIdx = ListView1.SelectedItem.Index
                ListView1.ListItems.Remove (ListView1.SelectedItem.Index)
            ElseIf ListView1.ListItems.Count = 0 Then
                MsgBox "Nothing to delete...", vbExclamation, "Delete"
            Else
                MsgBox "Please highlight the item to delete...", vbInformation, "Delete"
            End If
        End If
       
        ListView1.SetFocus  'Must have focus to display selected as highlighted
        ListView1.ListItems(lngIdx).Selected = True
        ListView1.ListItems(lngIdx).EnsureVisible   'Will display item if scroll has hidden
        
    End Sub


    Regards, OssieMac

    • Marked as answer by aslf010990 Monday, November 3, 2014 3:11 PM
    Sunday, November 2, 2014 10:45 AM
  • thank you very much worked perfectly , Greetings
    Monday, November 3, 2014 3:11 PM