SKINDER
SKINDER

Reputation: 1020

Is something like AfterSelectionChanged event exist for ListView?

I have a list of items in ListView and I need something like AfterSelectionChanged event. Now I am subscribed to ItemSelectionChanged but it is triggered N times if I press Ctrl+A in list with N items. But I need to be notified only one time after all items would be selected.

Thanks.

Upvotes: 0

Views: 542

Answers (2)

Branko Dimitrijevic
Branko Dimitrijevic

Reputation: 52147

private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
    if (listView1.SelectedItems.Count == listView1.Items.Count) {
        // All items selected.
    }
}

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 942267

You can make your own by delaying a method call until all the ItemSelectionChanged events stopped firing. Which is very cleanly done by Control.BeginInvoke(). Make it look similar to this:

    List<int> afterSelect = new List<int>();

    private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {            
        if (afterSelect.Count == 0) this.BeginInvoke(new Action(() => listView1_AfterSelectionChanged()));
        afterSelect.Add(e.ItemIndex);
    }

    private void listView1_AfterSelectionChanged() {
        // Use afterSelect
        //..
        afterSelect.Clear();
    }

You can use a bool flag instead of the List<> if you don't need to keep track of which items changed.

Upvotes: 1

Related Questions