This topic has not yet been rated - Rate this topic

Control.OnResize Method

Raises the Resize event.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
protected virtual void OnResize(
	EventArgs e
)

Parameters

e
Type: System.EventArgs
An EventArgs that contains the event data.

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.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Use OnResize instead of the Resize event when you can

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.