C# Anchor property doesn't seem to work

32.1k views Asked by At

I added some controls to my form and changed Anchor property how I'd expect this to work, but when I resize the form at the runtime, the controls stay at the same place.

For example, I have two buttons in bottom right corner of a form - they are on the form, no containers or anything like that. Anchor = Bottom, Right. FormBorderStyle = Sizable. But when I drag-resize the form while running, buttons do not move.

Am I missing something?

c# 2005

10

There are 10 answers

0
Jonathan Applebaum On

My problem was very simple,
all of my controls anchor properties was set correctly and contained inside a panel.
but i forgot to set anchor styles to the container panel so the container panel did not expand according to the form borders as i wanted...after setting anchor property of the container panel everything worked as expected.

1
user On

What is the Dock property set to? This can negate the anchor properties.

0
Daumantas Drukteinis On

If your form is localizable, check if you did any anchor/dock changes on other language.

0
sidereal On

I had this issue too. Just to add for what it's worth, check to see if autosize is set to true for all your child form controls.

0
porfirion On

I have the same problem in VS11 Beta. I used anchors a lot of times and it always worked properly, but now I can't understand what's going on with them and not only - dock fill doesn't work too! (no auto size or dock properties are used)

P.S. (after 40 minutes) Now it look's like I've found the problem: I have Resize event listener for PictureBox and I create new Image for new picturebox size in onResize handler. When I remove new image creation everything works!

Now I use SizeChanged event and in this event handler I create new image. So I think I shouldn't change sender object until Resize finished.

4
Oliver On

Another possibility would be that you accidentally placed your buttons not directly on the form. Instead you put them in some container (eg. panel, tableLayoutPanel, etc) and this container doesn't have set its anchoring or docking values correct.

Just to be absolutely sure you should take a look into designer.cs and check if your buttons are added directly to the form by this.Controls.Add() function or if they are added in any other Controls-List (eg. panel.Controls.Add()).

0
Bostwick On

Also if you have the auto size property set it will cause trouble.

0
Jack On

I also had a similar problem. I found that this was because I was resizing my form on form_load. This can be bypassed by temporarily docking to top/left while resizing the form

    private void ResizeFromDesigntimeToRunTime()
    {
        var volatileControls = this.Controls.Cast<Control>().Where(control => (control.Anchor | AnchorStyles.Bottom | AnchorStyles.Right) != AnchorStyles.None).ToList();

        var anchorPairing = volatileControls.ToDictionary(control => control, control => control.Anchor);

        foreach (var control in volatileControls)
            control.Anchor = AnchorStyles.Left | AnchorStyles.Top; //Temporarily reset all controls with an anchor including right or bottom, so that these aren't automatically resized when we adjust form dimensions.

        this.Height = SomeHeight;
        this.Width = SomeWidth;

        foreach (var pair in anchorPairing)
            pair.Key.Anchor = pair.Value;
    }
0
Roberto On

I know this an old post, but I'd like to try to contribute anyway.

My problem was that the form that I was adding into my panel didn't automatically adjust its size when the parent panel had its size changed.

The problem was that I was doing this:

form.WindowState = FormWindowState.Maximized; // <-- source of the problem
form.AutoSize = true; //this causes the form to grow only. Don't set it if you want to resize automatically using AnchorStyles, as I did below.
form.FormBorderStyle = FormBorderStyle.Sizable; //I think this is not necessary to solve the problem, but I have left it there just in case :-)
panel1.Controls.Add(form);
form.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)));
form.Dock = DockStyle.Fill; //this provides the initial size adjust to parent' size.
form.Visible = true;

To solve, I just commented the first line //form.WindowState = FormWindowState.Maximized; and everything worked like a charm.

0
Starceaker On

I had the exact same problem.

Situation:

TableLayoutPanel with one row set to autosize. In this row the anchoring Right, Bottom did NOT work. Removing the autoSize and putting it at a fixed height solved the problem, as prescribed by user428955.