471,319 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Question about handling form resizing events.

I have a WinForms based application written for the .NET Framework 2.0
and in this application I need to be able to be able to take some
action in code when the user finishes resizing the form.

I can easily create an event handler for the SizeChanged form level
event, the problem is if the user is using a mouse drag to resize the
form this event is firing every few milliseconds or what have you
until the user stops the drag process.

The approach I've taken is to create a Timer object and inside that
timer Tick event handler check the status of a flag that I'm setting
inside the form SizeChanged event handler. As I suspected, this a
sloppy approach that feels like a hack and it doesn't work without
bugs.

Can anyone recommend a proper way to do this?

Jun 22 '07 #1
6 4339
Check Form.ResizeEnd event

HTH
Alex
"JDeats" <Je**********@gmail.comwrote in message
news:11**********************@u2g2000hsc.googlegro ups.com...
>I have a WinForms based application written for the .NET Framework 2.0
and in this application I need to be able to be able to take some
action in code when the user finishes resizing the form.

I can easily create an event handler for the SizeChanged form level
event, the problem is if the user is using a mouse drag to resize the
form this event is firing every few milliseconds or what have you
until the user stops the drag process.

The approach I've taken is to create a Timer object and inside that
timer Tick event handler check the status of a flag that I'm setting
inside the form SizeChanged event handler. As I suspected, this a
sloppy approach that feels like a hack and it doesn't work without
bugs.

Can anyone recommend a proper way to do this?


Jun 22 '07 #2
JDeats,

I would extend the Form class and override the WndProc method. In it,
when you come across the WM_SIZE notification (it's value is 5, btw) you can
fire an event to indicate that the resizing operation has been completed.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"JDeats" <Je**********@gmail.comwrote in message
news:11**********************@u2g2000hsc.googlegro ups.com...
>I have a WinForms based application written for the .NET Framework 2.0
and in this application I need to be able to be able to take some
action in code when the user finishes resizing the form.

I can easily create an event handler for the SizeChanged form level
event, the problem is if the user is using a mouse drag to resize the
form this event is firing every few milliseconds or what have you
until the user stops the drag process.

The approach I've taken is to create a Timer object and inside that
timer Tick event handler check the status of a flag that I'm setting
inside the form SizeChanged event handler. As I suspected, this a
sloppy approach that feels like a hack and it doesn't work without
bugs.

Can anyone recommend a proper way to do this?

Jun 22 '07 #3
That works too!

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"AlexS" <sa***********@SPAMrogers.comPLEASEwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
Check Form.ResizeEnd event

HTH
Alex
"JDeats" <Je**********@gmail.comwrote in message
news:11**********************@u2g2000hsc.googlegro ups.com...
>>I have a WinForms based application written for the .NET Framework 2.0
and in this application I need to be able to be able to take some
action in code when the user finishes resizing the form.

I can easily create an event handler for the SizeChanged form level
event, the problem is if the user is using a mouse drag to resize the
form this event is firing every few milliseconds or what have you
until the user stops the drag process.

The approach I've taken is to create a Timer object and inside that
timer Tick event handler check the status of a flag that I'm setting
inside the form SizeChanged event handler. As I suspected, this a
sloppy approach that feels like a hack and it doesn't work without
bugs.

Can anyone recommend a proper way to do this?



Jun 22 '07 #4
On Jun 22, 3:29 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
JDeats,

I would extend the Form class and override the WndProc method. In it,
when you come across the WM_SIZE notification (it's value is 5, btw) you can
fire an event to indicate that the resizing operation has been completed.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"JDeats" <Jeremy.De...@gmail.comwrote in message

news:11**********************@u2g2000hsc.googlegro ups.com...
I have a WinForms based application written for the .NET Framework 2.0
and in this application I need to be able to be able to take some
action in code when the user finishes resizing the form.
I can easily create an event handler for the SizeChanged form level
event, the problem is if the user is using a mouse drag to resize the
form this event is firing every few milliseconds or what have you
until the user stops the drag process.
The approach I've taken is to create a Timer object and inside that
timer Tick event handler check the status of a flag that I'm setting
inside the form SizeChanged event handler. As I suspected, this a
sloppy approach that feels like a hack and it doesn't work without
bugs.
Can anyone recommend a proper way to do this?
Thanks that put me on the right track.... Since I needed to not only
detect when resizing ended but also when the maximize restore buttons
are pressed I had to resort to overriding WndProc anyway.

For anyone else looking to call a method whenever the user resizes the
form by mouse drag or by way of maximize restore buttons here, this
does work. Spent bout five hours on this issue.... There are days I
absolutely hate the .NET Framework team for not putting in enough
event handlers. It's seems like there might be many instances where
someone would want an event to trap on when the Form size is changed
or when the form has been maximized/restored.

Here's the code.
private bool inSizing = false;

private void OnFormResizeEnd()
{
// code to execute after sizing is done goes here.
}

protected override void WndProc(ref
System.Windows.Forms.Message m)
{

const int WM_SYSCOMMAND = 0x0112;
const int SC_MAXIMIZE = 0xF030;
const int SC_RESTORE = 0xF120;
const int WM_SIZING = 0x214;
const int WM_EXITSIZEMOVE = 0x232;

if (m.Msg == WM_SIZING)
{
inSizing = true;
}

if (m.Msg == WM_EXITSIZEMOVE && inSizing) //
WM_EXITSIZEMOVE
{
OnFormResizeEnd();
inSizing = false;
}

if (m.Msg == WM_SYSCOMMAND)
{
if ( (int)m.WParam == SC_MAXIMIZE)
{
OnFormResizeEnd();
inSizing = false;
}
}

if (m.Msg == WM_SYSCOMMAND)
{
if ( (int)m.WParam == SC_RESTORE)
{
OnFormResizeEnd();
inSizing = false;
}
}

base.WndProc(ref m);

}

Jun 22 '07 #5
On Fri, 22 Jun 2007 14:20:33 -0700, JDeats <Je**********@gmail.comwrote:
[...]
For anyone else looking to call a method whenever the user resizes the
form by mouse drag or by way of maximize restore buttons here, this
does work.
Thanks for posting the code for reference to others. One suggestion
though: IMHO, it would be better to use a switch() than to use several
independent if() statements. At the very least, use "else if()" for the
subsequent if() statements. :)
Jun 23 '07 #6
Or you can use the Form's SizeChanged event.

"JDeats" <Je**********@gmail.comwrote in message
news:11*********************@e9g2000prf.googlegrou ps.com...
On Jun 22, 3:29 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>JDeats,

I would extend the Form class and override the WndProc method. In
it,
when you come across the WM_SIZE notification (it's value is 5, btw) you
can
fire an event to indicate that the resizing operation has been completed.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"JDeats" <Jeremy.De...@gmail.comwrote in message

news:11**********************@u2g2000hsc.googlegr oups.com...
>I have a WinForms based application written for the .NET Framework 2.0
and in this application I need to be able to be able to take some
action in code when the user finishes resizing the form.
I can easily create an event handler for the SizeChanged form level
event, the problem is if the user is using a mouse drag to resize the
form this event is firing every few milliseconds or what have you
until the user stops the drag process.
The approach I've taken is to create a Timer object and inside that
timer Tick event handler check the status of a flag that I'm setting
inside the form SizeChanged event handler. As I suspected, this a
sloppy approach that feels like a hack and it doesn't work without
bugs.
Can anyone recommend a proper way to do this?

Thanks that put me on the right track.... Since I needed to not only
detect when resizing ended but also when the maximize restore buttons
are pressed I had to resort to overriding WndProc anyway.

For anyone else looking to call a method whenever the user resizes the
form by mouse drag or by way of maximize restore buttons here, this
does work. Spent bout five hours on this issue.... There are days I
absolutely hate the .NET Framework team for not putting in enough
event handlers. It's seems like there might be many instances where
someone would want an event to trap on when the Form size is changed
or when the form has been maximized/restored.

Here's the code.
private bool inSizing = false;

private void OnFormResizeEnd()
{
// code to execute after sizing is done goes here.
}

protected override void WndProc(ref
System.Windows.Forms.Message m)
{

const int WM_SYSCOMMAND = 0x0112;
const int SC_MAXIMIZE = 0xF030;
const int SC_RESTORE = 0xF120;
const int WM_SIZING = 0x214;
const int WM_EXITSIZEMOVE = 0x232;

if (m.Msg == WM_SIZING)
{
inSizing = true;
}

if (m.Msg == WM_EXITSIZEMOVE && inSizing) //
WM_EXITSIZEMOVE
{
OnFormResizeEnd();
inSizing = false;
}

if (m.Msg == WM_SYSCOMMAND)
{
if ( (int)m.WParam == SC_MAXIMIZE)
{
OnFormResizeEnd();
inSizing = false;
}
}

if (m.Msg == WM_SYSCOMMAND)
{
if ( (int)m.WParam == SC_RESTORE)
{
OnFormResizeEnd();
inSizing = false;
}
}

base.WndProc(ref m);

}

Jun 23 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Marinos Christoforou | last post: by
5 posts views Thread by ECVerify.com | last post: by
1 post views Thread by Natalia DeBow | last post: by
12 posts views Thread by Søren Reinke | last post: by
12 posts views Thread by scsharma | last post: by
11 posts views Thread by Ajith Menon | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.