T
Tim Mulholland
I'm having some weird issues when trying to use the Scale() method and
anchoring.
Here's the scenario.
I have a form. This form contains a user control. This user control contains
a button.
The button has all of its anchorstyle properties set (so its anchored on all
4 sides).
The form's resize code calls the Scale() method on the usercontrol.
This seems pretty straightforward, especially since both Scale and Anchor
are built-in.
However, the results i get are less than stellar. I'm hoping i'm just
missing something.
When i resize the form, the button inside the usercontrol does not stay the
same size as the user control. It seems pretty sporadic, but it seems that
at least one dimension of the button isn't big enough to fill the
usercontrol, while the other dimension is too big. Its not by a whole lot,
but its certainly noticable.
Any ideas? I'll include a tiny test (using Form1 and UserControl1) for
anyone that wants to try to duplicate the results. There's only about 10
lines to add after what the designer puts in there automatically.
Thanks in advance
-Tim
================Form1.cs================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ResizeTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private ResizeTest.UserControl1 userControl11;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private int ox;
private int oy;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.userControl11 = new ResizeTest.UserControl1();
this.SuspendLayout();
//
// userControl11
//
this.userControl11.BackColor = System.Drawing.Color.Firebrick;
this.userControl11.Location = new System.Drawing.Point(96, 80);
this.userControl11.Name = "userControl11";
this.userControl11.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(384, 339);
this.Controls.Add(this.userControl11);
this.Name = "Form1";
this.Text = "Form1";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Resize(object sender, System.EventArgs e)
{
foreach(Control c in this.Controls)
{
c.Scale((float)this.ClientSize.Width / (float)ox,
(float)this.ClientSize.Height / (float)oy);
c.Invalidate();
}
ox = this.ClientSize.Width;
oy = this.ClientSize.Height;
}
private void Form1_Load(object sender, System.EventArgs e)
{
ox = this.Width;
oy = this.Height;
}
}
}
======================================
==========UserControl1.cs=================
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace ResizeTest
{
/// <summary>
/// Summary description for UserControl1.
/// </summary>
public class UserControl1 : System.Windows.Forms.UserControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button btnResize;
public UserControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
btnResize = new Button();
Controls.Add(btnResize);
btnResize.Show();
btnResize.Location = new Point(0, 0);
btnResize.Size = this.ClientSize;
btnResize.Anchor = AnchorStyles.Bottom | AnchorStyles.Top |
AnchorStyles.Left | AnchorStyles.Right;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// UserControl1
//
this.BackColor = System.Drawing.Color.Firebrick; // Color set to make it
easier to see the user control
this.Name = "UserControl1";
}
#endregion
}
}
==================================
anchoring.
Here's the scenario.
I have a form. This form contains a user control. This user control contains
a button.
The button has all of its anchorstyle properties set (so its anchored on all
4 sides).
The form's resize code calls the Scale() method on the usercontrol.
This seems pretty straightforward, especially since both Scale and Anchor
are built-in.
However, the results i get are less than stellar. I'm hoping i'm just
missing something.
When i resize the form, the button inside the usercontrol does not stay the
same size as the user control. It seems pretty sporadic, but it seems that
at least one dimension of the button isn't big enough to fill the
usercontrol, while the other dimension is too big. Its not by a whole lot,
but its certainly noticable.
Any ideas? I'll include a tiny test (using Form1 and UserControl1) for
anyone that wants to try to duplicate the results. There's only about 10
lines to add after what the designer puts in there automatically.
Thanks in advance
-Tim
================Form1.cs================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ResizeTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private ResizeTest.UserControl1 userControl11;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private int ox;
private int oy;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.userControl11 = new ResizeTest.UserControl1();
this.SuspendLayout();
//
// userControl11
//
this.userControl11.BackColor = System.Drawing.Color.Firebrick;
this.userControl11.Location = new System.Drawing.Point(96, 80);
this.userControl11.Name = "userControl11";
this.userControl11.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(384, 339);
this.Controls.Add(this.userControl11);
this.Name = "Form1";
this.Text = "Form1";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Resize(object sender, System.EventArgs e)
{
foreach(Control c in this.Controls)
{
c.Scale((float)this.ClientSize.Width / (float)ox,
(float)this.ClientSize.Height / (float)oy);
c.Invalidate();
}
ox = this.ClientSize.Width;
oy = this.ClientSize.Height;
}
private void Form1_Load(object sender, System.EventArgs e)
{
ox = this.Width;
oy = this.Height;
}
}
}
======================================
==========UserControl1.cs=================
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace ResizeTest
{
/// <summary>
/// Summary description for UserControl1.
/// </summary>
public class UserControl1 : System.Windows.Forms.UserControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button btnResize;
public UserControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
btnResize = new Button();
Controls.Add(btnResize);
btnResize.Show();
btnResize.Location = new Point(0, 0);
btnResize.Size = this.ClientSize;
btnResize.Anchor = AnchorStyles.Bottom | AnchorStyles.Top |
AnchorStyles.Left | AnchorStyles.Right;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// UserControl1
//
this.BackColor = System.Drawing.Color.Firebrick; // Color set to make it
easier to see the user control
this.Name = "UserControl1";
}
#endregion
}
}
==================================