Problem changing the size property in the Resize event

G

Guest

I am trying to preserve the aspect ratio of a form when it is resized. To

simplify the problem, I'm starting with a square form, and trying to keep it

square when either the width or height is changed. My code is based on the

example found in MSDN, July 2005, for the Control.Resize Event.

Here is the relevant code:

public class Form1 : System.Windows.Forms.Form
{
private Size _previousSize;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
_previousSize = this.Size;
}

private void Form1_Resize(object sender, System.EventArgs e)
{
Control control = (Control) sender;

System.Console.WriteLine(
"Height={0}\tWidth={1}",
control.Size.Height, control.Size.Width);

Size newSize;
// Ensure the Form remains square (Height = Width).
if (control.Size.Height != _previousSize.Height)
{
System.Console.WriteLine(
"Height changed: oldSize={0}", _previousSize.ToString());

newSize = new Size(control.Size.Height, control.Size.Height);

System.Console.WriteLine("newSize={0}\n", newSize.ToString());

_previousSize = newSize; // save new value for previous size
control.Size = newSize;
}
else if (control.Size.Width != _previousSize.Width)
{
System.Console.WriteLine(
"Width changed: oldSize={0}", _previousSize.ToString());

newSize = new Size(control.Size.Width, control.Size.Width);

System.Console.WriteLine("newSize={0}\n", newSize.ToString());

_previousSize = newSize; // save new value for previous size
control.Size = newSize;
}
}
}

My code writes some debugging data to the console.

Here is the debugging data that is produced when I execute the Size command

from the control menu of my form, and press the right arrow key three times.

Height=300 Width=311
Width changed: oldSize={Width=300, Height=300}
newSize={Width=311, Height=311}

Height=311 Width=311
Height=300 Width=322
Height changed: oldSize={Width=311, Height=311}
newSize={Width=300, Height=300}

Height=300 Width=300

The first press of the right arrow key just starts the resize operation. On

the second press of the key, the code changes the Height from 300 to 311.

But on the third press of the key, the Height has changed back to 300 on

entry to the Resize event. That is the problem.

On the screen, the form alternates between increased sizes and its original

size.

Does anyone know what's causing this problem?
 
T

Truong Hong Thi

The question is: if both width and height change (like when dragging
the corner), what will you want the side of the square to be?
 
G

Guest

Truong Hong Thi said:
The question is: if both width and height change (like when dragging
the corner), what will you want the side of the square to be?

For now, I'm giving precedence to the height.
 
T

Truong Hong Thi

The problem might be because you reset Size, the event is reentered. I
did not try it, you'll have to debug it to see.

The following snippet should work as you expected:
private void Form1_Resize(object sender, System.EventArgs e)
{
if (WindowState != FormWindowState.Normal) return;
if (Height != _previousSize.Height)
{
Width = Height;
}
else
{
Height = Width;
}
_previousSize = this.Size;
}

Thi
 
G

Guest

Yes, I know for a fact that the Resize event is reentered when the Size is
reset. I know this because I have already debugged it, and I included the
debug output in my original post. The line of debug output that contains
"Height=311 Width=311 " represents the recursive re-entry of the Resize event
that occurs when the line of code "control.Size = newSize;" is executed.

But as I said in the original post, the real question is why the Height
changes back to 300 from 311 in the next line of debug output "Height=300
Width=322". That line is printed when I press the right-arrow key again which
increases the Width from 311 to 322 and fires the Resize event again. Since
the the Height was 311 on entry to the previous Resize event, how and why did
it revert to 300 on entry to the next Resize event? Is this due to a subtle
result of recursion, that I am failing to see, or an undocumented aspect of
the .NET Framework (I'm using version 1.1, by the way), or could it be, a bug
in the Framework?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top