Multimobile Development: Building Applications for any Smartphone
OnSizeChanged vs OnResize
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.windowsforms.controls.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

J.Marsch
What's the difference between OnSizeChanged and OnResize? I assume that
there must be some kind of behaviorial difference, or there wouldn't be two
sets of virtual + event to capture the same phenomena.

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Patrick Steele [MVP] (VIP)
In article <#Click here to reveal e-mail address>,
Click here to reveal e-mail address says...
[Original message clipped]

Resize: Occurs when the control is resized.

SizeChanged: Occurs when the Size property value changes.

My guess is that if I resize with the mouse, I'll get both events. If I
change the Size property, I'll only get the SizeChanged event.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Reply to this message...
Vote that this is a GOOD answer...
 
 
    
J.Marsch
No, I'm afraid that doesn't pan out.

If I capture both the Size and Resize events on a form, I find that both
events fire at runtime, regardless of whether I use the mouse or code to
change the size. Both events fire continuously during the mouse resize
operation.

Of course, your statement may be technically true, but using the mouse
resize a form still results in the Size property changing.

Here's the code that I used: (form with it's SizeChanged and Resize events
hooked, and a button that, when clicked, directly alters the Size property.
Same behavior either way).

private void Form1_SizeChanged(object sender, System.EventArgs e)
{
System.Diagnostics.Debug.WriteLine("SizeChanged");
}

private void Form1_Resize(object sender, System.EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Resize");
}

private void button1_Click(object sender, System.EventArgs e)
{
System.Drawing.Size size = this.Size;
size.Height += 5;
size.Width += 5;
this.Size = size;
}

"Patrick Steele [MVP]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Kevin Westhead
"J.Marsch" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

<snip>

It looks like you will always get both since OnResize is called from
OnSizeChanged. I'd recommend using ILDASM (part of the fx SDK) or
preferrably Reflector (http://www.aisto.com/roeder/dotnet/) to help you
inspect the internals of the fx assemblies. The following was taken from
Reflector:

protected virtual void OnSizeChanged(EventArgs e)
{
EventHandler handler1;
this.OnResize(EventArgs.Empty);

if (this.CanRaiseEvents)
{
handler1 = (base.Events[Control.EventSize] as EventHandler);

if (handler1 != null)
handler1.Invoke(this, e);

}

}

--
Kevin Westhead

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
J.Marsch
Thanks, Kevin.

I have now looked at both methods in Reflector.

So, it looks as though I can use either one, but I'd still love to know why
there are two. I wonder if there is some important nuance between the two.
If there is, it looks as though it would only come into play when overriding
the protected OnSizeChanged method, because the events themselves get fired
one after the other. Weird.

Anyone know exactly why one might need this degree of control over the
resize operation?

"Kevin Westhead" <kevinw@NOSPAM.i2DOTcoDOTuk> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Kevin Westhead
"J.Marsch" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
> So, it looks as though I can use either one, but I'd still love to know
why
> there are two. I wonder if there is some important nuance between the
two.
> If there is, it looks as though it would only come into play when
overriding
> the protected OnSizeChanged method, because the events themselves get
fired
> one after the other. Weird.

It could be down to usability rather than a technical reason. The Resize
event is no doubt familiar to VB6 developers whereas having a SizeChanged
event is consistent with .NET design guidelines, namely that if a property
value can be changed from code and from the GUI then a property-changed
event should be considered.

--
Kevin Westhead

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Ying-Shen Yu[MSFT] (VIP)
Hi all,

I'm trying to find someone to answer this question, Thanks for your
understanding!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Ying-Shen Yu[MSFT] (VIP)
Hi all,

Sorry for the long delay, I got the reply from the product team,
Simple answer:
From a user's point of view, they're the same thing. OnResize will be
called whenever OnSizeChanged is called. The reason we have both is that
OnSizeChanged is used for databinding and we had OnResize first. The user
can override either one and see
the same behavior.

Please reply this thread if you have anything unclear about it.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Patrick Steele [MVP] (VIP)
In article <Click here to reveal e-mail address>, v-
Click here to reveal e-mail address says...
[Original message clipped]

Thanks for investigating this! :)

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Reply to this message...
Vote that this is a GOOD answer...
 
 
    
J.Marsch
Ying-Shen:

Thank you for investigating this, even though it's not a problem, I'm glad
to know why we see the two events. Also, I'm sorry for the delayed
response -- I was on vacation last week.

-- Jeremy

""Ying-Shen Yu[MSFT]"" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
 
System.Diagnostics.Debug
System.Drawing.Size
System.EventArgs
System.EventHandler
System.Web.UI.Control






Multimobile Development: Building Applications for any Smartphone
Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Copyright © AMX Software Ltd 2008-2012. Portions copyright © Matthew Baxter-Reynolds 2001-2012. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734

Hi! 10 years ago I founded .NET 247. I am working on a new and exciting replacement for the site. Please join me on Twitter to discuss.

Thanks,
Matthew Baxter-Reynolds (@mbrit)

Follow mbrit on Twitter