使两个窗体控件与窗口均匀地调整大小

she*_*enk 3 c# windows-forms-designer winforms

我有一个在设计器中看起来像这样的表单,两个网格视图,完全相同的属性。它工作了一段时间,但是现在当我调整它的大小时,只有正确的网格视图水平扩展,它们都垂直扩展。锁定表单和控件也不会阻止我调整表单大小,这是最简单的解决方案。

是什么原因造成的?网格视图上唯一相关的属性是每个视图的上,右,左,下锚。请参阅底部的代码。

以下是一些屏幕截图:

这是Designer中的表单:

在设计器中

这是我尝试调整大小时的表格:

表格尺寸已调整

如您所见,右半部分更宽,我也无法正常调整其大小,因为我尝试对角线调整大小主要是垂直增长,而水平调整大小也可以做到这一点。我永远都遇到过调整大小的问题,但是最初两个gridview的大小都是一样的,我没有做任何更改,他们停了下来。我在这里想念什么吗?为什么不锁定表单会阻止其调整大小?我也锁定了每个控件。

以防万一,下面是设计器中网格视图的代码,首先是正确的代码:

    // clientHistoryTableDataGridView
    // 
    this.clientHistoryTableDataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
    | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right)));
    this.clientHistoryTableDataGridView.AutoGenerateColumns = false;
    this.clientHistoryTableDataGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
    this.clientHistoryTableDataGridView.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
    this.clientHistoryTableDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    this.clientHistoryTableDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
    this.dataGridViewTextBoxColumn4,
    this.dataGridViewTextBoxColumn5,
    this.dataGridViewTextBoxColumn6});
    this.clientHistoryTableDataGridView.DataSource = this.clientHistoryTableBindingSource;
    this.clientHistoryTableDataGridView.Location = new System.Drawing.Point(426, 52);
    this.clientHistoryTableDataGridView.Name = "clientHistoryTableDataGridView";
    this.clientHistoryTableDataGridView.RowHeadersVisible = false;
    this.clientHistoryTableDataGridView.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.True;
    this.clientHistoryTableDataGridView.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
    this.clientHistoryTableDataGridView.Size = new System.Drawing.Size(430, 360);
    this.clientHistoryTableDataGridView.TabIndex = 4;
    this.clientHistoryTableDataGridView.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.clientHistoryTableDataGridView_CellContentClick);
Run Code Online (Sandbox Code Playgroud)

和左侧:

    // clientTableDataGridView
    // 
    this.clientTableDataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
    | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right)));
    this.clientTableDataGridView.AutoGenerateColumns = false;
    this.clientTableDataGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
    this.clientTableDataGridView.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
    this.clientTableDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    this.clientTableDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
    this.dataGridViewTextBoxColumn1,
    this.dataGridViewTextBoxColumn2,
    this.dataGridViewTextBoxColumn3});
    this.clientTableDataGridView.DataSource = this.clientTableBindingSource;
    this.clientTableDataGridView.Location = new System.Drawing.Point(1, 52);
    this.clientTableDataGridView.Name = "clientTableDataGridView";
    this.clientTableDataGridView.RowHeadersVisible = false;
    this.clientTableDataGridView.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.True;
    this.clientTableDataGridView.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
    this.clientTableDataGridView.Size = new System.Drawing.Size(428, 360);
    this.clientTableDataGridView.TabIndex = 3;
    this.clientTableDataGridView.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.clientTableDataGridView_CellContentClick);
Run Code Online (Sandbox Code Playgroud)

编辑:

我通过同时使用两个答案解决了调整大小的问题。并在主窗体上禁用自动调整大小,并将borderstyle设置为“ sizable”。

ASh*_*ASh 5

可能的解决方案:

SplitContainer锚点Left|Top|Right|Bottom

设置SplitterDistance为SplitContainer宽度的一半

将clientTableDataGridView放在左侧面板中,并设置Dock = Fill

在右侧面板中放入clientHistoryTableDataGridView并设置Dock = Fill