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