如何将组件的大小链接到窗口大小? [英] How can I link a component's Size to the Window size?

查看:32
本文介绍了如何将组件的大小链接到窗口大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个简单的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.

我该怎么做?

推荐答案

使用

  • 将TableLayoutPanel添加到窗体
    • Add a TableLayoutPanel to the Form
    1. 编辑列和行"集合,以使您拥有2列(均设置为 50%)和1行(设置为 Autosize ).
    2. 将TableLayoutPanel的 Location.X 设置为 0 ,并将其宽度调整为Form的宽度.
    3. 将TLP Anchor 属性设置为 Left Right
    4. 将行高度调整为将其托管的Button的大小的两倍左右
    1. Edit the Columns and Rows collections so that you have 2 Columns, both sized at 50% and one Row, set to Autosize.
    2. Set the Location.X of the TableLayoutPanel to 0 and adjust its width to the width of the Form.
    3. Set the TLP Anchor property to Left and Right
    4. Adjust-drag the Row height to be ~twice the size of the Button it will host

  • 向表单添加一个按钮

  • Add one Button to the Form

    1. 根据需要调整按钮的外观.
    2. CTRL -拖动按钮以创建完全相同的副本
    3. 将两个按钮添加到TableLayoutPanel的两个单元格中
    4. Shift -选择两个按钮并将Dock属性设置为 DockStyle.Fill
    5. 您现在可以调整按钮的 Margin 属性(仍处于选中状态,因此对两个按钮都适用相同的设置)以修改控件之间的空间
    1. Adjust the appearance of the Button as required.
    2. CTRL-Drag the Button to create an exact duplicate
    3. Add the two Buttons to the two cells of the TableLayoutPanel
    4. SHIFT-Select both Buttons and set the Dock property to DockStyle.Fill
    5. 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

  • 根据需要重新调整TableLayoutPanel的唯一行高.
  • 额外:如果表单上只有这些控件,则可能需要修复表单的 MinimumSize ,以避免这种情况,在调整表单大小时,您的控件缩小到无法识别的程度,从而破坏了整体布局:在设计器中调整窗体的大小,使托管控件的布局受到损害,找到合适的最小尺寸,并将此度量用作窗体的

  • Re-adjust the TableLayoutPanel's only Row height as needed.
  • Extra: if you have only these controls on the Form, you may want to fix the 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.

    如果您稍微改动一下布局,则可能会发生这种情况.比尝试纠正问题更好的重新开始.

    TableLayoutPanel控件概述

    这篇关于如何将组件的大小链接到窗口大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆