例如,我在表单的右下角有两个按钮 - 它们在表单上,没有容器或其他任何东西。Anchor = Bottom, Right. FormBorderStyle = Sizable。但是当我在运行时拖动调整表单大小时,按钮不会移动。
我是否漏掉了什么?
c# 2005
另一个可能性是您意外地将按钮放置在表单之外。相反,您将它们放在某个容器中(例如panel、tableLayoutPanel等),而该容器没有正确设置其锚定或停靠值。
为了绝对确定,请查看designer.cs并检查您的按钮是否通过this.Controls.Add()
函数直接添加到表单中,或者它们是否添加到任何其他Controls-List中(例如panel.Controls.Add()
)。
我知道这是一篇老帖子,但我还是想尝试做出贡献。
我的问题在于,当父面板的大小改变时,我添加到面板中的表单并没有自动调整其大小。
问题在于我这样做:
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;
然后一切都可以完美工作了。如果您设置了自动大小属性,也会造成麻烦。
我在VS11 Beta中遇到了同样的问题。我经常使用锚点,它们总是正常工作,但现在我无法理解它们发生了什么事情,而且不仅如此-填充停止工作了!(没有使用自动大小或停靠属性)
P.S.(40分钟后) 现在看起来我已经找到了问题:我为PictureBox创建了Resize事件监听器,并在onResize处理程序中为新的picturebox大小创建新图像。当我删除新图像创建时,一切都正常工作!
现在我使用SizeChanged事件,在此事件处理程序中创建新图像。因此,我认为在调整大小完成之前不应更改发送方对象。
我的问题非常简单,
所有控件的锚定属性都设置正确并包含在一个面板中。
但我忘记了将容器面板的锚定样式设置为所需的形式边框大小,因此容器面板没有按照我想要的方式扩展...在设置容器面板的锚定属性后,一切都按预期工作。
我也遇到了这个问题。顺便提一下,检查一下所有子窗体控件是否设置为自动调整大小。
我遇到了完全相同的问题。
情况:
TableLayoutPanel
中有一行设置为 autosize
。在这一行中,右对齐和底部对齐的锚定没有起作用。
取消 autoSize
并将其设置为固定高度解决了问题,正如 user428955 所建议的那样。
我也遇到了类似的问题。我发现这是因为我在窗体加载时调整了窗体大小。可以通过在调整窗体大小时临时停靠到顶部/左侧来绕过此问题。
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;
}