Catching Unselecting All in ListView — SelectedIndexChanged Firing Twice

前端 未结 5 961
面向向阳花
面向向阳花 2021-01-19 21:37

Basically, I have a ListView of items. When one is selected, a text box comes into view on the right to display more details of that item (takes a little time for the item

相关标签:
5条回答
  • 2021-01-19 21:53

    In any control where you can select multiple items, SelectedIndexChanged event fires twice. One, to remove the deselected item and then to add selected item. When you click first time, selected items collection is null and hence it fired once. Next time it will fire twice.

    0 讨论(0)
  • 2021-01-19 21:54

    You can register to ListView's ItemSelectionChangedEvent instead.

            this.listView1.ItemSelectionChanged += this.HandleOnListViewItemSelectionChanged;
    
            private void HandleOnListViewItemSelectionChanged(Object sender, ListViewItemSelectionChangedEventArgs e)
            {
                if (e.IsSelected)
                {
                    this.detailsLabel.Text = this.GetDetails(e.Item);
                }
                else
                {
                    this.detailsLabel.Text = String.Empty;
                }
            }
    
    0 讨论(0)
  • 2021-01-19 21:55

    You can do something like this:

             private bool isInitialized = false;
             private void listView1_SelectedIndexChanged(object sender, EventArgs e) {
                 if (isInitialized) {
                     ListView.SelectedListViewItemCollection collection = this.listView1.SelectedItems;
    
                     if (collection.Count == 0) {
                         this.label2.Text = "Unselected all!";
                     }
                     foreach (ListViewItem item in collection) {
                         getSideInformation(item.Text);
                     }
                 }
                 isInitialized = true;
            }
    

    That will insure that first firing is ignored.

    0 讨论(0)
  • 2021-01-19 21:58

    The best solution I've found is to temporarily set an event handler to Application.Idle and do your checking from there, like so:

    bool handled;
    private void listView1_SelectedIndexChanged(object sender, EventArgs e) {
        if (!handled)
        {   handled = true;
            Application.Idle += SelectionChangeDone;   }
    }
    
    private void SelectionChangeDone(object sender, EventArgs e) {
        Application.Idle -= SelectionChangeDone;
        handled = false;
    
        ListView.SelectedListViewItemCollection collection = this.listView1.SelectedItems;
    
        if (collection.Count == 0)
            this.label2.Text = "Unselected all!"
    
        foreach (ListViewItem item in collection)
            getSideInformation(item.Text);
    }
    

    It shouldn't matter whether you use ItemSelectionChanged or SelectedIndexChanged. Both will work fine in this case.

    Big thanks goes to Grammarian for his answer to essentially the same question here: https://stackoverflow.com/a/26393234/2532220

    0 讨论(0)
  • 2021-01-19 21:59

    you can use the below code inside the event block to check whether the SelectedIndexChange event is firing directly because of the control or due to some postback event from some other usercontrols/Pages

    string id= Request.Form["__EVENTTARGET"];
    
    if(!string.IsNullorEmpty(id))
    {
    //your code here
    }
    
    0 讨论(0)
提交回复
热议问题