C # Anchor property doesn't seem to work

I added some controls to my form and changed the Anchor property, as I would expect it to work, but when I resize the form at run time, the controls stay in one place.

For example, I have two buttons in the lower right corner of the form - they are on the form, without containers or something like that. Anchor = bottom, right. FormBorderStyle = Sizable. But when I drag the form during operation, the buttons do not move.

Did I miss something?

C # 2005

+7
source share
9 answers

Another possibility is to randomly place your buttons directly on the form. Instead, you put them in a container (like a panel, tableLayoutPanel, etc.), and this container does not set the correct binding or docking values.

To be absolutely sure that you should take a look at designer.cs and check if your buttons are added directly to the form using the this.Controls.Add() function or if they are added to any other list of controls (e.g. panel.Controls.Add() ).

+11
source

I know this old post, but I would still like to contribute.

My problem was that the form that I added to my panel did not automatically resize it when I resized the parent panel.

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 on the first line //form.WindowState = FormWindowState.Maximized; and everything worked like a charm.

+5
source

Also, if you have an auto size property, this will cause problems.

+4
source

What is a dock property? This may negate binding properties.

+3
source

I have the same problem in VS11 Beta. I used the anchors many times, and it always worked correctly, but now I can’t understand what is happening with them, and not only - filling the dock does not work either! (no auto size or dock properties are used)

PS (after 40 minutes) Now it looks like I found the problem: I have a Resize event listener for the PictureBox, and I'm creating a new image for the new image size in the onResize handler. When I delete a new image creation, everything works!

Now I use the SizeChanged event and in this event handler I create a new image. Therefore, I think that I should not change the sender object until the resizing is complete.

+1
source

I had the same problem.

Situation:

TableLayoutPanel with a single row set to autosize . On this line, the Right binding, Bottom did NOT work. Removing autosize and setting it to a fixed height solves the problem as instructed by the autosize .

0
source

My problem was very simple:
all the binding properties of my controls were correctly set and contained inside the panel.
but I forgot to set the anchor styles on the container panel so that the container panel does not expand to fit the boundaries of the form, as I wanted ... after setting the container panel binding property, everything worked as expected.

0
source

If your form is localizable, check to see if you have made any binding / pinning changes in another language.

0
source

I also had a similar problem. I found this to be because I resized my form to form_load. This can be circumvented by temporarily docking to the top / left edge when 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
source

All Articles