How to override Form.Show () method in C #

I would like to override a method Show()

for a form, but C # doesn't allow me to do this. I want to do this to update the list when I return to the previous window. I don't want to use observable collection etc.

 protected override void Show() {
            base.Show();
            list.Items.Clear();
            loadListItems();
        }

      

'Sklep.OknoProduktow.Show ()': cannot override inherited element 'System.Windows.Forms.Control.Show ()' because it is not marked virtual, abstract or overridden

+3


source to share


3 answers


Why not use an activated event?

This will help you. When your form is activated, your list will be updated.



I think you cannot override show()

and therefore this did not solve your problem.

private void Form1_Activated(object sender, System.EventArgs e)
{
  list.Items.Clear();
  loadListItems();
}

      

+4


source


You need to differentiate between events and methods. Overriding an event is not possible if it is not declared virtual. The Form.Shown event is not virtual or secure.

There is another mechanism for overriding event handling in Winforms. Each Xxxx event has a corresponding protected method named OnXxxx. The responsibility of the OnXxxx () method is to fire the Xxxx event, ultimately taking care of the implementation in the base class. In this case Form.OnShown ().

The implication of this method is that you have additional options for how you want to override the default event handling:

  • You can call base.OnShown () first and then do whatever you want to set up for the event. This will allow you to override anything a custom event handler could have done. Common in an OnPaint event handler, for example when you want to make sure that whatever you paint is on top of anything written in the base class or event handler.
  • You can write your custom code and then call base.OnShown (). To achieve the opposite goal, a custom event handler can override whatever you've done. This is the normal way.
  • You can write your custom code and not call base.OnShown (). This prevents event handlers from firing. Rare for this, but a choice you want to make when your setup is so extensive that your client code's event handler might crash.
  • You can write your own custom code, rather than call base.OnShown (), but still fire the Shown event. Rare, but you should do this if the underlying implementation gets in the way.


So the most likely correct implementation of your method is:

    protected override void OnShown(EventArgs e) {
        loadListItems();
        //Call the original OnShown.
        base.OnShown(e);
    }

      

Uses a 2nd bullet, letting the event handler customize the list you have loaded.

Trying not to confuse the big problem, you are doing it wrong. The list must be initialized in the form's constructor.

+10


source


You cannot override the Show method, but you can shadow it. Place this code in your form class:

public new void Show()
{
     //your code here

     //call the shadowed Show method on our form.       
     base.Show();

}

      

+8


source







All Articles