Groups | Blog | Home
all groups > dotnet windows forms > august 2005 >

dotnet windows forms : Anchor Layout Oversizes Controls


Ian Evitable
8/27/2005 12:00:00 AM
Hello

I have controls on a winform that use top,botton left achors. Once i save
the form and close and reopen it (in design mode) the controls are all
oversized such that half of them disappear beneath the panel/tabcontrols in
which they are contained. This occurs with both user controls and toolbox
controls. I want my controls to resize but only in proportion to the form. I
have no idea why these are oversizing themselves but then end result is that
the form is useable.

TIA
Ian

Tim_Mac
8/29/2005 6:28:34 AM
hi ian,
it's a bit annoying, but it only really works to have one control
anchored to the bottom. it does make sense when you think about it,
although it would be nice if there was a 'distribute size evenly' mode
for a container layout.
the behaviour you are seeing happens because when the form stretches,
all the other controls stretch because they are told to keep the same
distance from the bottom of the form. they then extend over the tops
of the controls beneath (which are anchored to the top, so the tops
always stay at the same place), which results in the overlap you are
seeing.
you need to decide which control will do the stretching, and keep the
others in to a fixed height, presumably anchored to the top and left.
you can listen out for the Form_Resize event, and programatically set
the sizes of the controls. this isn't too hard. ask for code sample
if you would like one.

tim
Ian Evitable
9/1/2005 12:00:00 AM
Hi Tim,

Thanks for your reply but i think you misunderstand the situation. The
controls are "walking" right off/under the bottom of the containing
container.
I appreciate that one control above another will yield the behaviour your
referring too, if both are anchored to the top and bottom.

Im talking about a situation where i have a tab control anchored to the all
four sides fo the form. On the various tab pages i have common controls and
usercontrols that are anchored to the top and bottom of the containing tab
page. Once i save, close, reopen or run the form, the controls are
overstretched such that they disappear under the tab page "bottom"
margin..... even though the tab page itself correctly scales according to
the form size. In other words if the tab page scales by 15% the usercontrols
might well scale 65%.

Ian



[quoted text, click to view]

Tim_Mac
9/1/2005 12:00:00 AM
p.s. note that autoscroll is false on all the containers, and it works as
expected.
tim

--------------------------
blog: http://tim.mackey.ie

Tim_Mac
9/1/2005 6:29:52 AM
hi Ian,
can you post some code?
i tried to create a sample form like the one you describe. and the
scaling works fine. the code i used is below. maybe you could copy it
into a form in VS and see if it looks like your situation?
tim

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Test
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.CheckedListBox checkedListBox1;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.button1 = new System.Windows.Forms.Button();
this.listView1 = new System.Windows.Forms.ListView();
this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Anchor =
((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Location = new System.Drawing.Point(0, 0);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(536, 400);
this.tabControl1.TabIndex = 0;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.checkedListBox1);
this.tabPage1.Controls.Add(this.listView1);
this.tabPage1.Controls.Add(this.button1);
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Size = new System.Drawing.Size(528, 374);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
//
// button1
//
this.button1.Anchor =
((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(0, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(528, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
//
// listView1
//
this.listView1.Anchor =
((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.listView1.Location = new System.Drawing.Point(8, 272);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(512, 96);
this.listView1.TabIndex = 1;
//
// checkedListBox1
//
this.checkedListBox1.Anchor =
((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.checkedListBox1.Location = new System.Drawing.Point(56, 48);
this.checkedListBox1.Name = "checkedListBox1";
this.checkedListBox1.Size = new System.Drawing.Size(400, 199);
this.checkedListBox1.TabIndex = 2;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(536, 396);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.Text = "Form1";
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
AddThis Social Bookmark Button