Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Resize Event: Get old size?

Tags:

c#

.net

winforms

I've added a resize event to one of my widgets, which looks like this:

void glControl_Resize(object sender, EventArgs e) {

Is there a way I can get the old size of the widget (before resizing)? Maybe I can cast e to something that will give me more info? Or should I just save it during that event?

like image 456
mpen Avatar asked Jan 14 '10 00:01

mpen


People also ask

What is resize event in Windows Forms?

Resize Event System. Windows. Forms Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here. Occurs when the control is resized. The following code example handles the Resize event of a Form.

What is the difference between resize and sizechanged events?

The Resize event occurs when the control is resized, while the SizeChanged event occurs when the Size property changes. You could use either, as a resize will cause the Size property to change.

What is size changed event in Windows system?

System. Windows. Forms Control. Size Changed Event System. Windows. Forms Occurs when the Size property value changes. The following code example demonstrates the SizeChanged event. An instance of a Button control has been provided that can be scaled both horizontally and vertically.

When does the resize event fire?

The resize event fires when the document view (window) has been resized. In some earlier browsers it was possible to register resize event handlers on any HTML element. It is still possible to set onresize attributes or use addEventListener () to set a handler on any element.


2 Answers

Yes, just tracking the old size in a class field is the simple solution. For example:

Size mOldSize;

private void glControl_Resize(object sender, EventArgs e) {
  if (mOldSize != Size.Empty && mOldSize != glControl.Size) {
    // do something...
  }
  mOldSize = glControl.Size;
}
like image 140
Hans Passant Avatar answered Oct 11 '22 09:10

Hans Passant


By convention you should add an OnResizing event, which fires just when it is about to change but hasn't changed, and then you fire the OnResize after it has been resized. You would get the old value from your EventArg in the OnResizing event.

Edit:

Are you creating your own event or firing one of an included control?

If you are doing your own event, you can derive from EventArg and make something like ResizeEventArg that include the size of the thing you want.

I would use the ResizeEventArg for both the Resize and OnResizing events, and still follow what I said earlier.

Or if you know which type of control it is, you could cast the Object sender into the type and then read the property.

like image 44
Francisco Noriega Avatar answered Oct 11 '22 09:10

Francisco Noriega