Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# detect windows really resized instead moved

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...

like image 965
ghiboz Avatar asked Jun 25 '14 09:06

ghiboz


2 Answers

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..

like image 132
TaW Avatar answered Sep 30 '22 01:09

TaW


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.

like image 30
Syed Farjad Zia Zaidi Avatar answered Sep 29 '22 23:09

Syed Farjad Zia Zaidi