Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listview ItemSelectionChanged fires twice?

I have a Winforms App in C# with a ListView control. This ListView shows a list of TO-DO items and I am using the 'ItemSelectionChanged' event to handle updates.

The problem is that the 'ItemSelectionChanged' event fires twice each time I try to make an update.

The ItemSelectionChanged event refreshs the form to represent the updates (ie remove item from list).

Is there a way to disable the event from firing after the refresh?

UPDATE1:

private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
    {   
        if (e.IsSelected)
        {                
            listView1.Items[e.ItemIndex].Remove();

            listView1.SelectedIndices.Clear();
            listView1.Focus();

            listView1.Update();
        }
        else
        {

        }

    }
like image 674
John M Avatar asked Jul 12 '10 14:07

John M


1 Answers

Yes, it will fire twice. Once because the previously selected item became unselected, again for the newly selected item. You just have to make sure you see the selection event:

    private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
        if (e.IsSelected) {
            // Update form
            //...
        }
    }
like image 80
Hans Passant Avatar answered Oct 27 '22 02:10

Hans Passant