Listview ItemSelectionChanged fires twice?
Asked Answered
M

4

11

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
        {

        }

    }
Melvamelvena answered 12/7, 2010 at 14:51 Comment(0)
W
19

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
            //...
        }
    }
Westfalen answered 12/7, 2010 at 17:5 Comment(2)
thanks but the IsSelected didn't work for me. There must be something else about my code that keeps it firing twice.Melvamelvena
No, it will definitely fire twice. This is by design. The point is to detect the one that you are interested in. Which isn't that clear from your question.Westfalen
N
0

I think you need manually unselect the item in the end of your handler.

listView1.SelectedItem = null;

Narial answered 16/12, 2017 at 5:46 Comment(0)
C
-1

Yes just remove the EventHandler at the start of the refresh and add it again after it has finished refreshing

i.e

// Remove handler
listView1.ItemSelectionChanged -= new ListViewItemSelectionChangedEventHandler(listView1_ItemSelectionChanged);

// Do refresh

// Add again
listView1.ItemSelectionChanged += new ListViewItemSelectionChangedEventHandler(listView1_ItemSelectionChanged);
Cloudless answered 12/7, 2010 at 14:54 Comment(8)
Do I need any additional references for the 'OnSelectionChanged'?Melvamelvena
No, You just need to replace ListView with the name of your ListViewControl i.e. ListView1 and MethodName should be the name of the function that is called when the OnSelectionChanged event is fired i.e. ListView1_OnSelectionChanged where ListView1 is again the name of your ListViewControlCloudless
For me the 'OnSelectionChanged' doesn't show up in Intellisense.Melvamelvena
I had it slightly off, I've updated the example to make it clearer. You just need to replace listView1 with the name of your listview and the same for listView1_ItemSelectionChanged. Let me know if you get stuckCloudless
It should go in the refresh method, the remove at the start and the add at the bottom. If that doesnt work post the code and I'll take a look for youCloudless
How does Collection -= new thing remove anything from a collection? You're asking the collection to remove something that was never added to it.Lawford
@Lotharyx It doesn't, it's not a collectoin it's an event handler. It's removed to stop the event firing whilst updating, and then adds it back again afterwards.Cloudless
@w69rdy An event handler is a collection (of delegates). I know what removing the delegate does; what I'm saying is your example code doesn't remove anything because you're removing a new delegate instance that was never added in the first place.Lawford
H
-1

Try this:

private void ItemSelect()
{

        if(SelectedItem!=null)
            App.Current.MainPage.Navigation.PushAsync(new Pages.TLAccByCurrency(), true);
        _selectedItem = null;
}
Hilde answered 10/5, 2018 at 8:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.