ListView Events (C# .NET 2.0)

Cyrilix

2[H]4U
Joined
Jan 21, 2005
Messages
2,188
I have a question about the ListView events, since they do not seem to do what I'd like them to do. Essentially, I'm populating a ListView with a bunch of strings, and then selecting different items. Every time an item is selected, I'd like the ListView to notify me, regardless of whether or not the item has changed. Now, here's the problem. Say, I also have a ComboBox in my form. First, I click on an item in the ListView (SelectedIndexChanged event fires). Then, I click on the ComboBox, which should deselect the ListView item. Then, I click on the same ListView item again (SelectedIndexChanged does not fire). How should I make it so that an event notifies me of such a case, even if I'm clicking on the same item?

Well, I thought that I'd go and experiment a few things before I asked so I tried:

-ItemActivate event with ActivationMode set to OneClick (so that a single click on an item should fire this event). The problem with this event, however, is that there seems to be a very long lag time (almost a second) between when I click the item and when the event fires. This is unacceptable.

-SelectedItemChanged event. This works the same way as SelectedIndexChanged (for my purposes).

Is there anything else I should do?

I'm considering just handling the Click event and on every click, I'd check to see whether or not an item from the ListView is selected (by looking at ListView.SelectedIndices). This doesn't seem to be as efficient, but it may be the only way.

Thanks.
 
Without really knowing some info about the items being presented in the ListView control and the ability of selecting single or multiple items, I'd suggest either:
1) monitor the "Click" event handler like you suggested, or
2) monitor the "ItemSelectionChanged" event handler

Your decision here really depends more on how this ListView object is being utilized and how the control's properties are set. But with the information presented, I think one of the above two options should take care of the issue with a few lines of code.
 
Now that I reread again what ItemSelectionChanged is supposed to be, I think I might be able to use it. I'll give it a try and see how it goes.
 
Back
Top