8
votes

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

9
It's always worked for me in the past, so I would guess that you are doing something wrong. Can you post a code sample (check in the designer.cs file) - or a screen shot possibly that demonstrates the issue.Simon P Stevens
Add a new form, add a button, and set the Anchor to Bottom, Right. If this actually works, it means that there are something wrong in the way things are done in your original form - see answers from others for figuring it out. If the new form does not work, I have NO idea what's wrong...awe

9 Answers

12
votes

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()).

5
votes

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.

4
votes

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

3
votes

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

1
votes

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.

0
votes

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.

0
votes

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.

0
votes

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

0
votes

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;
    }