Resize/Scale/Anchor problems

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
}
}
==================================
 
R

Rhett Gong [MSFT]

Hi Tim,
I build a project according to your code. However, in my machine, the
button is scaled as expected. Could you test your code in other machine to
see if you still get the same problem please?
I made the test with a XP sp1 box and .Net 1.1 framework.


Thanks,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
T

Tim Mulholland

I've done it on several machines (all XPSP1, .NET 1.1), all to the same
effect.
The button scaling isn't a very noticable problem, but it is noticable.

This was a scaled down project just to illustrate the problem, but the
problem is considerably worse in our actual program we're working on.
Perhaps i could e-mail you the entire project to look at or something?
 
R

Rhett Gong [MSFT]

I've done it on several machines (all XPSP1, .NET 1.1), all to the same
effect.
The button scaling isn't a very noticable problem, but it is noticable.
This was a scaled down project just to illustrate the problem, but the
problem is considerably worse in our actual program we're working on.
Perhaps i could e-mail you the entire project to look at or something?
I made a test with your code in your first post. I did not change anything
in my side. If it requires other actions on the project, please let me know.

Since you have isolated the problem in a "a scaled down project", just send
that to me please. If possible, some screen captures on this problem would
be better.

To send e-mail to me, please remove online from my email address in the
newsgroup.


Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 

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