Control.OnResize Method
Raises the Resize event.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.
The OnResize method also enables derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.
Notes to Inheritors
When overriding OnResize in a derived class, be sure to call the base class's OnResize method so that registered delegates receive the event. The OnResize method can be called during construction, so if you override OnResize it can be called before the control constructor is called.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
If you create a user control and need to modify your layout when the control changes, override the OnResize method instead of attaching to the control's Resize event. This allows you to make changes to components contained on the control before the control is layed out, and can give you a better visual experience.
The following methods both do the same thing, but the override keeps the horizontal scroll bar from flashing on and off.
protected override void OnResize(EventArgs e)
{
label1.Width = this.Width - 14;
base.OnResize(e);
}
private void MyControl_Resize(object sender, EventArgs e)
{
label1.Width = this.Width - 14;
}
The purpose of both is to make the label width use up the width of the client area of the control, but the second causes the horizontal scrollbar to flash on and off during resizing, sometimes leaving the scrollbar visible. The first changes the label width before the control is checked to see if any of its components go off the edge.
It's probably a good idea always to override event handlers on controls, when that's possible. The only time you need to attach an event handler with event handler logic is when the control needs a call back method to another object within your application.
- 11/3/2010
- Gil Yoder