简体   繁体   English

为什么在显示或加载事件时我的表单大小无法正确调整?

[英]Why isn't my form resized correctly when on shown or load event?

I have a form with: 我有一个表格:

DataGrid
SaveButton
CancelButton

Positioned one under another. 一个接一个地定位。 I call the folowing method when the form is shown and resized: 当表单显示并调整大小时,我称为folowing方法:

private void resize(object sender, EventArgs e)
        {
            int totalRowHeight = thresholdsDataGridView.ColumnHeadersHeight;

            foreach (DataGridViewRow row in thresholdsDataGridView.Rows)
                totalRowHeight += row.Height;

            thresholdsDataGridView.Height = totalRowHeight; 
            this.Height = totalRowHeight + closeButton.Height + saveChangesButton.Height;
        } 

This works well when resizing but when showing it it does not add enough height (the bottom button is not shown). 调整大小时效果很好,但显示时并不能增加足够的高度(未显示底部按钮)。 Can someone shed some light on this? 有人可以阐明这一点吗?

Screenshot of the desired result: 所需结果的屏幕截图:

在此处输入图片说明

It seems that you have every control placed over each other. 似乎您已将所有控件彼此放置在一起。 To make this design simpler, set: 为了简化设计,请设置:

Buttons' anchors to: "Bottom, Left, Right" and DataGridView's anchor to: "Top, Bottom, Left, Right" in designer. 在设计器中,Buttons的锚定为: “下,左,右” ,DataGridView的锚定为: “顶部,下,左,右”

Then add this variable to your form's class' root: 然后将此变量添加到表单类的根目录:

int originalHeight;

Set the "originalHeight" in your form's constructor: 在表单的构造函数中设置“ originalHeight”:

originalHeight = this.Height;

Then each time a new row is added, run: 然后,每次添加新行,请运行:

private void resize(object sender, EventArgs e)
{
   int rowsHeight = thresholdsDataGridView.Rows.GetRowsHeight(DataGridViewElementStates.Visible);
   this.Height = originalHeight + rowsHeight;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM