C# 锚属性似乎不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1600049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Anchor property doesn't seem to work
提问by flamey
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.
我在表单中添加了一些控件并更改了 Anchor 属性,我希望它如何工作,但是当我在运行时调整表单大小时,控件保持在同一个位置。
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.
例如,我在表单的右下角有两个按钮——它们在表单上,没有容器或类似的东西。锚点 = 底部,右侧。FormBorderStyle = 可观。但是当我在运行时拖动调整表单的大小时,按钮不会移动。
Am I missing something?
我错过了什么吗?
c# 2005
C# 2005
回答by user
What is the Dock property set to? This can negate the anchor properties.
Dock 属性设置为什么?这可以否定锚属性。
回答by Oliver
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.
另一种可能性是您不小心将按钮放置在表单上。相反,您将它们放在某个容器(例如面板、tableLayoutPanel 等)中,并且该容器没有正确设置其锚定或停靠值。
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()
).
为了绝对确定您应该查看designer.cs并检查您的按钮是否按this.Controls.Add()
功能直接添加到表单中,或者它们是否添加到任何其他控件列表中(例如panel.Controls.Add()
)。
回答by Bostwick
Also if you have the auto size property set it will cause trouble.
此外,如果您设置了自动大小属性,则会导致麻烦。
回答by Roberto
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.
为了解决这个问题,我只是评论了第一行//form.WindowState = FormWindowState.Maximized;
,一切都像魅力一样。
回答by Starceaker
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.
TableLayoutPanel
一行设置为autosize
. 在这一行中,锚定 Right, Bottom 不起作用。autoSize
按照用户 428955 的规定,将其移除并将其放置在固定高度解决了该问题。
回答by porfirion
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)
我在 VS11 Beta 中遇到了同样的问题。我多次使用锚点并且它总是正常工作,但现在我无法理解它们发生了什么,不仅 - 码头填充也不起作用!(不使用自动大小或停靠属性)
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!
PS(40 分钟后)现在看起来我已经找到了问题:我为 PictureBox 设置了 Resize 事件侦听器,并在onResize
处理程序中为新的图片框大小创建了新图像。当我删除新图像创建时,一切正常!
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.
现在我使用SizeChanged
事件,并在此事件处理程序中创建新图像。所以我认为在调整大小完成之前我不应该更改发件人对象。
回答by jonathana
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.
我的问题很简单,
我所有的控件锚属性都设置正确并包含在面板中。
但我忘记为容器面板设置锚点样式,因此容器面板没有按照我想要的表单边框展开……设置容器面板的锚点属性后,一切都按预期工作。
回答by Daumantas Drukteinis
If your form is localizable, check if you did any anchor/dock changes on other language.
如果您的表单是可本地化的,请检查您是否对其他语言进行了任何锚点/停靠更改。
回答by Hyman
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
我也有类似的问题。我发现这是因为我在 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;
}