c# detect windows really resized instead moved
Asked Answered
H

4

5

In my winform program I need to detect when the form is resized: but the ResizeEndmethod is also called when the form is simply moved into the desk..

Is it possible check only when the windows is only resized??

In my mind I can save the last width and the last height and into the ResizeEnd method like this:

int lastWidth;
int lastHeigth;
private void frmMain_ResizeEnd(object sender, EventArgs e)
{
    if (lastHeigth != this.Height || lastWidth != this.Width)
    {
        lastHeigth = this.Height;
        lastWidth = this.Width;
        fireResize();
    }
}

But this is an ugly solution...

Husain answered 25/6, 2014 at 9:4 Comment(0)
P
8

Only marginally better than your original solution, but at least it adresses the problem instead of just quoting the docs.

Obviousy the problem is that Resize fires all the time, so a flag seems to be necessary:

bool sizing = false;
private void Form1_ResizeEnd(object sender, EventArgs e)
{
    if (!sizing) return;
    if (sizing) {sizing = false; /*do your stuff*/ }
}

private void Form1_Resize(object sender, EventArgs e)
{
    sizing = true;
}

Of course it would be nice to have an indicator in the EventArgs of ResizeEnd but can't see a simpler way to do it.

BTW, instead of checking Width and Height using Size would also be a small improvement..

Pneumatology answered 25/6, 2014 at 9:29 Comment(0)
M
2

Why not use this? This works fine for me...

 public Form1()
 {
     this.Resize += Form1_Resize;
 }

 void Form1_Resize(object sender, EventArgs e)
 {
     // do what you want to do 
 }

Here read this from MSDN

The ResizeBegin event is raised when the user begins to resize a form, typically by clicking and dragging one of the borders or the sizing grip located on the lower-right corner of the form. This action puts the form into a modal sizing loop until the resize operation is completed. Typically, the following set of events occurs during a resize operation:

  • A single ResizeBegin event occurs as the form enters resizing mode.

  • Zero or more pairs of Resize and SizeChanged events occur as the form's Size is modified.

  • A single ResizeEnd event occurs as the form exits resizing mode.

Note:

Just clicking without dragging on a border or resizing grip will generate the ResizeBegin and ResizeEnd events without any intermediate Resize and SizeChanged event pairs.

The ResizeBegin and ResizeEnd pair of events is also raised when the user moves the form, typically by clicking and dragging on the caption bar. These events are not generated by programmatic manipulation of the form, for example by changing the Size or Location properties.

Marr answered 25/6, 2014 at 9:11 Comment(0)
H
0

How about checking the documentation? The second search engine hit is the following.

Form.ResizeBegin Event - MSDN

The ResizeBegin event is raised when the user begins to resize a form, typically by clicking and dragging one of the borders or the sizing grip located on the lower-right corner of the form. This action puts the form into a modal sizing loop until the resize operation is completed. Typically, the following set of events occurs during a resize operation:

  1. A single ResizeBegin event occurs as the form enters resizing mode.
  2. Zero or more pairs of Resize and SizeChanged events occur as the form's Size is modified.
  3. A single ResizeEnd event occurs as the form exits resizing mode.

Just clicking without dragging on a border or resizing grip will generate the ResizeBegin and ResizeEnd events without any intermediate Resize and SizeChanged event pairs.

The ResizeBegin and ResizeEnd pair of events is also raised when the user moves the form, typically by clicking and dragging on the caption bar. These events are not generated by programmatic manipulation of the form, for example by changing the Size or Location properties.

Hoatzin answered 25/6, 2014 at 9:11 Comment(0)
B
0

Use simple Resize event. It's only triggered on resizing.

Boccioni answered 25/6, 2014 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.