Listview ItemSelectionChanged How to Undo / Undo

If you look at the code below. The Listview ItemSelectionChanged event fires twice and so I get the UserClosedSession dialog twice. Is there a way to suppress the second dialogue?

Basically I am trying to do some validation when the user clicks on an item in the Listview. When changing the selected item, they are asked a question, if yes, then go and select a new item, if not, then "deselect" the selection. The code below is a simple example of a real world problem only. Ignore the contents of the UserClosedSession, it is simply used here to simulate what should happen.

I've tried all kinds. Tested mouse and mouse events. But this is not good if the user changes the selection using the keyboard. I also tried routing and then reattaching the ItemSelectionChangedevent to itself (see code with comments) - doesn't work.

    private void lv_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
    {
        if (e.IsSelected)
        {
            Debug.WriteLine("Index: " + lv.SelectedIndices[0].ToString());
            if (lv.Tag != null)
            {
                if ((int)lv.Tag != lv.SelectedIndices[0])
                {

                    if (!UserClosedSession())
                    {
                        //lv.ItemSelectionChanged -= new ListViewItemSelectionChangedEventHandler(lv_ItemSelectionChanged);
                        //lv.ItemSelectionChanged -= lv_ItemSelectionChanged;
                        lv.Items[(int)lv.Tag].Selected = true;
                        //lv.ItemSelectionChanged +=new ListViewItemSelectionChangedEventHandler(lv_ItemSelectionChanged);
                        return;
                    }
                }
            }
            else
                lv.Tag = lv.SelectedIndices[0];
        }
    }


    private bool UserClosedSession()
    {
        return
            (MessageBox.Show("Close Session?", "", MessageBoxButtons.YesNo) == DialogResult.Yes);
    }

      

+3


source to share


1 answer


I've used something like this before and it worked, maybe it will be for you in this situation. All I do is set a boolean field to hold a value indicating whether this event was handled or not. If it has not been processed, the method will work and set the processed field to true. Next time, it will see that it was processed rather than passed through the entire method, and set the processed field back to false. This will only happen if the event is fired twice in a row and you only want to handle the fire of the first event. If you want a second event fire, I can help you. If this event fires more than 2 times in a row, let me know, I may also be able to find a solution to this problem.



bool eventHandled = false;
private void lv_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
    if (!eventHandled)
    {
        eventHandled = true;
        if (e.IsSelected)
        {
            Debug.WriteLine("Index: " + lv.SelectedIndices[0].ToString());
            if (lv.Tag != null)
            {
                if ((int)lv.Tag != lv.SelectedIndices[0])
                {
                    if (!UserClosedSession())
                    {
                        //lv.ItemSelectionChanged -= new ListViewItemSelectionChangedEventHandler(lv_ItemSelectionChanged);
                        //lv.ItemSelectionChanged -= lv_ItemSelectionChanged;
                        lv.Items[(int)lv.Tag].Selected = true;
                        //lv.ItemSelectionChanged +=new ListViewItemSelectionChangedEventHandler(lv_ItemSelectionChanged);
                        return;
                    }
                }
            }
            else
                lv.Tag = lv.SelectedIndices[0];
        }
    }
    else
        eventHandled = false;
}

      

0


source







All Articles