0

I run into a strange problem (WinXP / .NET 2.0). I use a WinForm UserControl that overrides Refresh():

public override void Refresh()
    {
        DoSomeStuff();
        base.Refresh();
    }

I add this UserControl as child to another control and want to refresh all child controls:

ParentControl : UserControl
{
  [...]

  public ParentControl (...)
  {
    [...]

    UserControl ChildControl = ModelEngine.MainControl; // UserControl as mentioned above
    this.Controls.Add(ChildControl);

    [...]

    ModelEngine.MainControl.Refresh(); //#1
    this.Refresh(); // #2
  }
}

Calling the Refresh() method directly (#1) works fine. I expected that I can call Refresh() on the parent class (#2) and this would trigger a recursive Refresh() on all child controls (as explained in MSDN http://msdn.microsoft.com/en-us/library/system.windows.forms.control.refresh.aspx). However, the overridden Refresh() in the child control is not executed. BTW: setting ControlStyles.UserPaint to true didn't change the behaviour.

Of course I could call Refresh() directly (as in #1) or write my own recursive Refresh(). But I am wondering whether this bug is an indication of a bigger problem somewhere in my code...

So is there an obvious error in my code or is this the regular behaviour of .NET?

Jay
  • 56,361
  • 10
  • 99
  • 123

1 Answers1

1

As it says in the page to which you linked:

Notes to Inheritors

When overriding Refresh in a derived class, be sure to call the base class's Refresh method so the control and its child controls are invalidated and redrawn.

You must call the base Refresh() method explicitly. Otherwise, there would be no way to not run the base method, and the whole concept of overrides would be lost.

Jay
  • 56,361
  • 10
  • 99
  • 123
  • In his ParentControl class, he doesn't seem to override the base Refresh() method, so it looks like he *is* calling the base method when using "this". – Ocelot20 Feb 23 '11 at 17:03
  • @Ocelot20 That is true, but he is calling a virtual method in the constructor. See http://stackoverflow.com/questions/119506/virtual-member-call-in-a-constructor – Jay Feb 23 '11 at 19:47
  • My apologies, didn't realize it was a virtual method. – Ocelot20 Feb 23 '11 at 20:26