Cher
Cher

Reputation: 2947

How to only trigger event when an item is selected (ListView)?

I've been using this property of a ListView:

SelectedIndexChanged

However, it gets triggered also when item is unselected.

What is the best event if I only want event to be triggered when an item is actually selected, and to be called only once? Not twice in a row like ItemActivate.

Upvotes: 0

Views: 268

Answers (1)

Xi Sigma
Xi Sigma

Reputation: 2372

You can use the SelectedItems.Count property in your ListView, return whenever its 0, or handle the event if its greater than 0, so all you need is an if statement in your event handler such as

if(yourListView.SelectedItems.Count == 0)
    return;

//Do your thing

Or:

if(yourListView.SelectedItems.Count > 0){
   //Do your thing
}

Upvotes: 1

Related Questions