3

I have a ListView class that uses the ItemSelectionChanged event to maintain its own list of selected items.

This is working well, except that Clear() doesn't seem to fire the ItemSelectionChanged event.
This means that after Clear() is called the list still contains all of the items that were highlighted prior to Clear() being called.

I wanted to override the Clear() method so that I could clear the list myself, but I can see that it's not marked as virtual. Is there another way that I can use the existing Clear() method?

Plan B is that I just write my own method and call it right after clear, but that isn't as nice.

listView1.Clear();
listView1.ClearMyList();
Equalsk
  • 7,954
  • 2
  • 41
  • 67

2 Answers2

4

actually I dont know the name of this feature of c# (public new void), but you can do this by creating your own "customListView".

var listView = new CustomListView();
listView.Clear();

public class CustomListView : ListView
{
    public new void Clear()
    {
        Console.WriteLine("I can clear myself...");
    }
}

enter image description here

Bruno Joaquim
  • 1,423
  • 1
  • 14
  • 15
0

You can not override non virtual method. So writting your 'new' own method is the best way to go.

This is a similar question

Community
  • 1
  • 1
Mathieu Schmitt
  • 676
  • 6
  • 7