WinForms Anchor Control Changes Place of Origin?

I ported my C # /. NET 2.0 project to Mono for use on other platforms, but this seems to be causing a problem in the NATIVE WinForms implementation.

I have isolated the issue for the relationship between the control (specifically the Button) Anchor and the Y component property of the location. If the AnchorStyle property is set to Top, the origin of the Location property is the ClientArea of ​​the form (excluding the title bar). Changing the anchor to the bottom, however, changes the origin to the top-left corner of the entire window (including the title bar).

Here's a small form class that illustrates the difference:

public class RawCodeForm : Form
    public RawCodeForm()
        Button b = new Button();
        b.Text = "Test";
        b.Location = new Point( 10, 10 );
        b.Size = new Size( 75, 23 );
        b.Anchor = AnchorStyles.Left | AnchorStyles.Top;
        //b.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;

        this.Controls.Add( b );
        this.Size = new Size( 100, 200 );
        this.Location = new Point( 100, 100 );


Try swapping b.nchor lines to see the change in position.

Is this a documented bug or am I missing another property that needs to be set?

Edit: Thanks for pointing out that the form starts at size (300 300). I assumed it was (0,0) until installed.

Outside of the simple test form above, the problem now is that changing the FormBorderStyle later causes the form to resize. My guess is that in Mono (or host operating system) the resizable FormBorderStyle resizes the ClientArea smaller, where - since the ClientSize remains the same size in native WinForms.


source to share

1 answer

This is because you resized the form after adding the button. Change it before

this.Size = new Size(100, 200);
this.Location = new Point(100, 100);

Button b = new Button();
b.Text = "Test";
b.Location = new Point(10, 10);
b.Size = new Size(75, 23);
//b.Anchor = AnchorStyles.Left | AnchorStyles.Top;
b.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;



The button simply follows the change to the bottom border when it is anchored to the bottom.



All Articles