locked
Listview event when clicked RRS feed

  • Question

  • Hi all, 

    Using VB 2010. I am working on a form with a listview, configured as List, that displays basic customer data in tabular form. Along with the listview I have a Remove button which is initially disabled. When the user clicks on a listview row the Remove button is enabled and if clicked, the selected listview item is removed from the list.

    The issue I am having is that the user can click on a listview row, thus enabling the Remove button then click on an empty area of the listview, thus unselecting the item. The Remove button stays enabled and if clicked no item is removed. This is the expected behavior, but I would like the Remove button to disable if the user clicks on the empty area of the listview. Another acceptable solution is to not have the row be deselected when this region is clicked.

    Here is an image showing the list populated with three items. I clicked on an item enabling the Remove button then I clicked the empty area. This deselected item, but left the Remove button enabled.

    After monitoring events I can't seem to locate an event that fires when the listview as a whole is clicked. My code at this point is the following:

      Private Sub lvwCst_Click( _
             ByVal sender As Object, _
             ByVal e As System.EventArgs) _
      Handles lvwCst.Click
    
        'User selected an item. Make sure it is valid and if it is enable the remove button.
    
        'Check if any items have been selected.
        If lvwCst.SelectedItems Is Nothing Then
          'None, disable remove button.
          btnDelete.Enabled = False
        Else
          'Looks like an item was selected. If exactly 1 then enable button, else disable. 
          btnDelete.Enabled = lvwCst.SelectedItems.Count() = 1
        End If
    
      End Sub
    
      Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
    
        'Remove item from the list.
        For Each li As ListViewItem In lvwCst.SelectedItems
          lvwCst.Items.Remove(li)
        Next
    
      End Sub
    

    I have Multiselect set to False, so there should be only one item selected at any given time when the user clicks on an item.

    When I click the empty area and the previously selected item is deselected lvwResources.SelectedItems.Count() goes to zero. This is good, but I do not know how to use it to disable the Remove button. Any help with this issue is welcomed. Thank you for your time and assistance. Saga


    Insanity is the prelude to discovery

    Thursday, April 17, 2014 7:58 PM

Answers

  • Try the SelectedIndexChanged event

        Private Sub lvwCst_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvwCst.SelectedIndexChanged
            btnDelete.Enabled = lvwCst.SelectedItems.Count > 0
        End Sub


    Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com

    • Marked as answer by SagaV9 Thursday, April 17, 2014 8:21 PM
    Thursday, April 17, 2014 8:15 PM

All replies

  • Try the SelectedIndexChanged event

        Private Sub lvwCst_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvwCst.SelectedIndexChanged
            btnDelete.Enabled = lvwCst.SelectedItems.Count > 0
        End Sub


    Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com

    • Marked as answer by SagaV9 Thursday, April 17, 2014 8:21 PM
    Thursday, April 17, 2014 8:15 PM
  • Awesome! Worked. This platform is just full of surprises! Thank you. Saga

    Insanity is the prelude to discovery

    Thursday, April 17, 2014 8:21 PM