C# Anchor 属性似乎不起作用

发布于 2024-08-08 05:51:10 字数 218 浏览 6 评论 0原文

我向表单添加了一些控件,并更改了 Anchor 属性,但当我在运行时调整表单大小时,控件仍保持在同一位置。

例如,我在表单的右下角有两个按钮 - 它们位于表单上,没有容器或类似的东西。锚点 = 底部,右侧。 FormBorderStyle = 可调整大小。但是,当我在运行时拖动调整表单大小时,按钮不会移动。

我错过了什么吗?

时间:2005-03-07 标签:c#2005

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

囍孤女 2024-08-15 05:51:10

另一种可能性是您不小心将按钮没有直接放在表单上。相反,您将它们放入某个容器(例如 panel、tableLayoutPanel 等)中,而该容器没有正确设置其锚定或对接值。

为了绝对确定,您应该查看 Designer.cs 并检查您的按钮是否通过 this.Controls.Add() 函数直接添加到表单中,或者是否添加到任何其他控件中-列表(例如panel.Controls.Add())。

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

薄荷梦 2024-08-15 05:51:10

我知道这是一篇旧帖子,但无论如何我想尝试做出贡献。

我的问题是,当父面板的大小更改时,我添加到面板中的表单不会自动调整其大小。

问题是我正在这样做:

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;

为了解决这个问题,我只是注释了第一行 //form.WindowState = FormWindowState.Maximized; ,一切都很顺利。

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.

残月升风 2024-08-15 05:51:10

此外,如果您设置了自动调整大小属性,也会造成麻烦。

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

沉默的熊 2024-08-15 05:51:10

Dock 属性设置为什么?这可以否定锚点属性。

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

信仰 2024-08-15 05:51:10

我在 VS11 Beta 中也遇到同样的问题。我使用锚很多次,它总是工作正常,但现在我不明白它们发生了什么,不仅 - 码头填充也不起作用! (不使用自动大小或停靠属性)

PS(40 分钟后)
现在看来我已经发现了问题:我有 PictureBox 的 Resize 事件侦听器,并且我在 onResize 处理程序中为新的图片框大小创建了新图像。当我删除新图像创建时,一切正常!

现在我使用 SizeChanged 事件,并在此事件处理程序中创建新图像。所以我认为在调整大小完成之前我不应该更改发送者对象。

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.

蹲在坟头点根烟 2024-08-15 05:51:10

我也有这个问题。只是为了添加它的价值,请检查所有子表单控件的 autosize 是否设置为 true。

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.

妥活 2024-08-15 05:51:10

我遇到了完全相同的问题。

情况:

TableLayoutPanel 将一行设置为 autosize。在这一行中,锚定右、下不起作用。
按照 user428955 的规定,删除 autoSize 并将其放置在固定高度解决了该问题。

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.

清风不识月 2024-08-15 05:51:10

我的问题很简单,
我的所有控件锚点属性均已正确设置并包含在面板内。
但我忘记为容器面板设置锚点样式,因此容器面板不会根据表单边框展开正如我想要的......设置容器面板的锚属性后,一切都按预期工作。

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.

请爱~陌生人 2024-08-15 05:51:10

如果您的表单可本地化,请检查您是否对其他语言进行了任何锚​​点/停靠更改。

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

紫﹏色ふ单纯 2024-08-15 05:51:10

我也有类似的问题。我发现这是因为我在 form_load 上调整了表单的大小。可以通过在调整表单大小时临时停靠到顶部/左侧来绕过此问题

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

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文