对接多个控件-一个可填充剩余空间 [英] Docking multiple controls - one fills remaining space
问题描述
我正在尝试将N个控件停靠在一个容器中.我希望它们全部填满整个宽度,但要堆叠.我希望一个特定的控件(当前是最后一个)填充剩余的空间,而所有其他控件的大小都固定.
此:
Button b1 = new Button(){文本="Button 1",Dock = DockStyle.Top};按钮b2 = new Button(){文本=按钮2",Dock = DockStyle.Top};按钮b3 = new Button(){文本=按钮3",Dock = DockStyle.Fill};Controls.Add(b1);Controls.Add(b2);Controls.Add(b3);
产生这个:
如您所见,Button 3完全按照我的指示执行: Fill
父级.但这不是我想要要做的.除了手动放置和处理调整大小事件等以外,如何使Button 3填充剩余空间?
注意:我不是使用设计器.
在添加 b3.BringToFront()
(将其添加到 Controls
之后)的同时,这里最简单的解决方案是简单地更改将按钮添加到 Controls
的顺序.以下代码可以完美运行:
Button b1 = new Button(){文本="Button 1",Dock = DockStyle.Top};按钮b2 = new Button(){文本=按钮2",Dock = DockStyle.Top};按钮b3 = new Button(){文本=按钮3",Dock = DockStyle.Fill};Controls.Add(b3);//这个家伙先!Controls.Add(b1);Controls.Add(b2);
结果:
如果您仔细看一下这个小示例中的边框,它实际上比 BringToFront()
效果更好.
I'm trying to dock N number of controls in a container. I want them all to fill the entire width, but stack. I want one particular control (currently the last one) to fill the remaining space, while all others have fixed sizes.
This:
Button b1 = new Button() { Text = "Button 1", Dock = DockStyle.Top };
Button b2 = new Button() { Text = "Button 2", Dock = DockStyle.Top };
Button b3 = new Button() { Text = "Button 3", Dock = DockStyle.Fill };
Controls.Add(b1);
Controls.Add(b2);
Controls.Add(b3);
Produces this:
As you can see, Button 3 is doing exactly what I told it to: Fill
the parent. But that's not what I want it to do. Aside from manually placing, and handling resize events, etc. how can I make Button 3 fill the remaining space?
Note: I am not using the designer.
While adding b3.BringToFront()
(after it has been added to Controls
) works, the simplest solution here, is to simply change the order in which the buttons are added to Controls
. The following code works perfectly:
Button b1 = new Button() { Text = "Button 1", Dock = DockStyle.Top };
Button b2 = new Button() { Text = "Button 2", Dock = DockStyle.Top };
Button b3 = new Button() { Text = "Button 3", Dock = DockStyle.Fill };
Controls.Add(b3); // this guy first!
Controls.Add(b1);
Controls.Add(b2);
The result:
If you take a close look at the borders in this little example, this actually seems to work better than BringToFront()
.
这篇关于对接多个控件-一个可填充剩余空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!