Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

listview control and selectedIndexChanged event

Status
Not open for further replies.

gacaccia

Technical User
May 15, 2002
258
0
0
US
i'm trying to use a listview control. i need to use the selectedIndexChanged event and was calling a debug.writeline in the event to confirm its behavior when clicking between items. i was surprised to find that each time i click on a new item in the listview control, the selectedIndexChanged event is getting fired twice. once with a selected index count of 0 and then a selected index count of 1.

does anyone know if this is normal behavior for this control and event? more of an issue for me is how the selectedIndexChanged event fires when clicking on a single item after having previously selected multiple items using the ctrl key. in this case, it fires three times, once for an item from the previously selected group, once for no items selected and once for the newly selected item.

in this case, how are you suppose to distinguish between the first firing that is associated with an item that is in fact not selected with the third firing that is for the correct item?

thanks,

glenn
 
The SelectedIndexChanged event occurs in single selection ListView controls, whenever there is a change to the index position of the selected item.

In a multiple selection ListView control, this event occurs whenever an item is removed or added to the list of selected items.

If you need to determine whether the user has added to or removed from the collection of selected items, you could declare a variable at the class level to store the SelectedItems.Count property. Each time the event is fired, compare the current SelectedItems.Count to the class level variable to determine if an item has been added or removed.

Code:
Private iLastCount As Integer
Private Sub ListView1_SelectedIndexChanged _
             (ByVal sender As Object, _
              ByVal e As System.EventArgs) _
              Handles ListView1.SelectedIndexChanged
    If iLastCount < ListView1.SelectedItems.Count Then
        MsgBox(&quot;you added an item to the selectedItems collection&quot;)
    Else
        MsgBox(&quot;you removed an item from the selectedItems collection&quot;)
    End If
    iLastCount = ListView1.SelectedItems.Count
End Sub

If you would let us know what you are trying to accomplish, perhaps we could be of more help.

What are you doing during the selectedIndexChanged event?

-Stephen Paszt
 
stephen,

thanks for the reply. there are two levels that i'm working on here. on the one level, i have a simple task of creating two listview controls containing parent and child items. the parent listview will populate first and then based on the item selected by the user, the second listview will populate with other items.

on a more general level, i'm trying to understand how the different control events behave. my concern is that many of the events do not behave as one would expect. for example, you stated that the selectedIndexChanged event occurs whenever there is a change to the index position of the selected item (single select mode). what i was trying to say in my first post is that more than this, the event will actually fire twice whenever there is a change to the index position.

i made a simple test where i created a form with a single listview control. i created a list of five items and added a procedure for the selectedIndexChanged event. there is only one line of code in the event...

Code:
    Private Sub lvOne_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvOne.SelectedIndexChanged
        Debug.WriteLine(&quot;selected index changed&quot;)
    End Sub

if i run this in debug mode, click on item one, then item two and then item three, the selectedIndexChanged event fires five times! once for the initial selection, and then twice for every selection thereafter. now, i'm using visual studio.net 2002 with the initial .net framework (prior to 1.1), so i don't know if the behavior is different with the newer .net framework. if you try this test, do you not get the same results?

it's not that i can't get the process to work with the two listview controls, but as you load on more controls with more interactions between them, duplicate firings of an event can cause real problems.
 
I'm also using the 1.0 framework, but I'm pretty sure the ListView.SelectedIndexChanged hasn't changed in V1.1.

When I run the code in the above post, with the listview's MultiSelect property set to true, I do get the same results. The thing here is that a listview, with the MultiSelect property set to true, will fire the SelectedIndexChanged whenever an item is removed or added to the list of selected items. This, to me, seems correct.

In your particular case, to get around the multiple events, use the MouseUp event. Here's the code I tested:

Code:
Private Sub ListView1_MouseUp(ByVal sender As Object, _
           ByVal e As System.Windows.Forms.MouseEventArgs) _
           Handles ListView1.MouseUp
    If ListView1.SelectedItems.Count > 0 Then
        Dim str = (&quot;You selected:&quot; & vbCrLf)
        Dim i As Integer
        For i = 0 To ListView1.SelectedItems.Count - 1
            str = str & ListView1.SelectedItems(i).Text & vbCrLf
        Next
        MsgBox(str)
    Else
        MsgBox(&quot;Nothing selected&quot;)
    End If
End Sub

I was going to suggest using the click event, but the click event is only raised when you click an item and not the background, to clear the selection for example, hence the MouseUp event.

Hope this helps,

-Stephen Paszt
 
i hadn't thought about the mouseup event. i ended up implementing a different solution, though i may revisit it to compare with the mouseup approach.

basically, my confusion was due to my assumption that the selectedIndexChanged event corresponded to clicks in the listview box, so that if one item were selected and i clicked on a new item, the event would fire once. instead, the selectedIndexChanged event fires once for the automatic deselection of the first item and once for the result of the user click (for single item selection).

likewise, when in multiselect mode, if you select four different items so that all are selected and then do a regular click on a new item so that only one item is selected, the selectedIndexChanged event will fire five times. four times for each item that was auto deselected in the process of clicking on a new item and once for the new item selected.

my solution was to have the selectedIndexChanged event save the selecteditems.count value each time it fires. if the prior count = 0 and the current count = 1, i know that the reported selected item by the event is the item currently highlighted by the user. for my purposes, if the count is higher than 1 i can ignore it.

glenn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top