Make two form controls resize with window evenly

5.6k views Asked by At

I have a form that looks like this in designer, two grid views, exactly the same properties. It worked for a bit, but now when I resize it, only the right grid view expands horizontally, they both expand vertically. Also locking the form and controls doesn't stop me from resizing the Form which would be easiest solution.

What could cause this? The only relevant properties on the Grid Views are anchors for Top, Right, Left, Bottom on each. See code at bottom.

Here are some screenshots:

Here is the form in Designer:

In Designger

And here is the form when I try to resize it:

Form resized

As you can see the right half is wider, I also cannot resize it normally, as I try a diagonal resize it mainly grows vertically, a horizontal resize does the same thing. I've had the resizing issue forever, but the two gridviews were both resizing equally at first, I made no changes and they stopped. Am I missing something here? Why doesn't locking the form stop it from being resizable? I locked every control as well.

Just in case, here is the code for the grid views in the designer, first the right one:

    // 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);

and the left side:

    // 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);

EDIT:

I fixed the resizing issues by using both answers together. And also disabling autosize on the main form and setting borderstyle to sizable.

2

There are 2 answers

4
ASh On BEST ANSWER

possible solution:

add SplitContainer with anchor Left|Top|Right|Bottom

set SplitterDistance at half of SplitContainer width

put clientTableDataGridView in left panel and set Dock = Fill

put clientHistoryTableDataGridView in right panel and set Dock = Fill

4
Andrey Korneyev On

You can resolve this problem in the following manner:

  1. Set anchors for both gridview as Top | Bottom.
  2. Override OnResize method of your form and in this method set width and location if both gridviews manually like this:

protected override void OnResize(EventArgs e)
{
    base.OnResize(e);

    var width = ClientRectangle.Width / 2;
    clientTableDataGridView.Left = 0;
    clientTableDataGridView.Width = width;
    clientHistoryTableDataGridView.Left = width;
    clientHistoryTableDataGridView.Width = width;
}

And of course, instead of setting anchors in step 1 - you can completly manage gridviews size in OnResize. Here I've mentioned that step just for simplicity.