如何将组件的大小链接到窗口大小? [英] How can I link a component's Size to the Window size?
问题描述
我正在制作一个简单的WinForms程序.
我想将组件的大小链接到Window的大小.
I am making a simple WinForms program.
I would like to link a component's size to the size of the Window.
比方说,用户可以通过拖动窗口的边框来放大或缩小Window:我希望当Window运行时组件会变大,反之亦然.
Let's say the user enlarges or shrinks the Window by dragging it's borders: I would like that a component gets bigger when the Window does and vice versa.
假设我们在窗口的中心并排有两个按钮:我想使它们的大小相同,以填充窗口的整个宽度.
Let's pretend that we have two Buttons, in the center of the Window, side by side: I want to make them the same size filling the entire width of the Window.
我该怎么做?
推荐答案
- 根据需要调整按钮的外观.
- CTRL -拖动按钮以创建完全相同的副本
- 将两个按钮添加到TableLayoutPanel的两个单元格中
- Shift -选择两个按钮并将Dock属性设置为
DockStyle.Fill
- 您现在可以调整按钮的
Margin
属性(仍处于选中状态,因此对两个按钮都适用相同的设置)以修改控件之间的空间
- Adjust the appearance of the Button as required.
- CTRL-Drag the Button to create an exact duplicate
- Add the two Buttons to the two cells of the TableLayoutPanel
- SHIFT-Select both Buttons and set the Dock property to
DockStyle.Fill
- You can now adjust the
Margin
property of the Buttons (still both selected, so the same settings will apply to both) to modify the space between the controls
MinimumSize
,以避免这种情况,在调整表单大小时,您的控件缩小到无法识别的程度,从而破坏了整体布局:在设计器中调整窗体的大小,使托管控件的布局受到损害,找到合适的最小尺寸,并将此度量用作窗体的
MinimumSize
of the Form, to avoid that, when the Form is resized, your controls are shrinked beyond recognition, ruining the overall layout: resize the Form in the Designer to a point where the hosted controls layout is compromised, find a suitable minimum size and use this measure as the Form's MinimumSize
property. The MinimumSize
can be set using only the Width
or the Height
measure (e.g., (100, 0)
). This limits the Form's Width
but not the Height
. Or the opposite, of course.如果在拖动TableLayoutPanel内的按钮时,按钮不会自动插入到单元格的左上角位置而是将它们显示在随机的位置,然后TableLayoutPanel具有 rogue流氓,需要放下.删除它并在表单上放置另一个.冲洗并重复.
If, when dragging the Buttons inside the TableLayoutPanel, the Buttons are not automatically inserted at the Top-Left position of the cell and instead they appear positioned in a random place, then the TableLayoutPanel has gone rogue and needs to be put down. Delete it and drop another on the Form. Rinse and repeat.
如果您稍微改动一下布局,则可能会发生这种情况.比尝试纠正问题更好的重新开始.
这篇关于如何将组件的大小链接到窗口大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!