[英]How can I make a control take 80% of the parent's height
I know you can dock an item to cover the bottom, top, left, right, or all of the form. 我知道您可以停靠一个项目以覆盖底部,顶部,左侧,右侧或所有表单。 But I want to have a datagridview cover 80% of the height of the forum and all of the width and cannot figure out how to do that.
但是我想让datagridview覆盖论坛高度的80%和所有宽度,并且无法弄清楚该怎么做。
You can use the TableLayoutPanel
( MSDN ) to accomplish this, and can be done entirely in the designer. 您可以使用
TableLayoutPanel
( MSDN )完成此操作,并且可以完全在设计器中完成。
Add a TableLayoutPanel
and have it fill you entire form. 添加一个
TableLayoutPanel
并使其填满整个表单。 Then, create 2 rows in the panel and give the first row a percentage value of 80% for height (Your options are percentage, pixels, and auto-fill). 然后,在面板中创建2行,并为第一行的高度百分比设置为80%(您的选项是百分比,像素和自动填充)。 Finally, move your
DataGridView
into the first row and dock it to the entire row. 最后,将
DataGridView
移到第一行并将其停靠到整行。
You can use the docking (eg Top
or Bottom
) to setup the width part of your problem, however, to synchronize the height you'll need to programmatically calculate it (and keep it synced). 您可以使用对接(例如
Top
或Bottom
)来设置问题的宽度部分,但是,为了同步高度,您需要以编程方式计算它(并使其保持同步)。
There's an example here that sets the width so that idea will apply: https://stackoverflow.com/a/18384344/33969 . 这里有一个设置宽度的示例,这样想法就可以应用: https : //stackoverflow.com/a/18384344/33969 。 You'll want to call that function on form load, but also as an event handler for the Form.Resize event .
您将要在表单加载时调用该函数,还要将其作为Form.Resize event的事件处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.