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.Us开发者_JAVA技巧erPaint 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?
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.
精彩评论