Make two form controls resize with window evenly
Asked Answered
O

2

7

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.

Odaodab answered 5/6, 2015 at 8:53 Comment(3)
If you want to you can use a SplitContainer and set FixedPanel=None and IsSPlitterFixed=true; also SplitterWidth=1 and the rest is working automatically. . Other than that you will have to code the resize event.Lepsy
"Locking" is only a design feature, so when you are designing your form, you can't accidentally move it. It sounds like you should have these two grids dock-filled inside a TableLayoutPanel, then you won't need any of this resizing code you are doing. If you just don't want the form resizable, change the FormBorderStyle property of the form.Tetragon
I got it working, but thanks for the form border style tip.Odaodab
F
12

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

Fiery answered 5/6, 2015 at 9:14 Comment(4)
how do I put the grid views in the splitcontainers?Odaodab
@shenk, not in splitContainers, but in panels of the single splitContainer. in visual designer move a grid inside panel. designer generated code will be somthing like this.splitContainer1.Panel1.Controls.Add(this.clientTableDataGridView); this.splitContainer1.Panel2.Controls.Add(this.clientHistoryTableDataGridView);Fiery
I seem to be getting odd exceptions in Unknown module now. The code this.clientHistoryTableTableAdapter.Fill(this.archiveAddinDataSet.ClientHistoryTable); this.clientTableTableAdapter.Fill(this.archiveAddinDataSet.ClientTable); seems to be causing problems now, it's in Form_Load()Odaodab
Worked perfect, although i'm having the resizing issues on the window. Doubt it's from the splitcontainer.Odaodab
O
4

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.

Ostler answered 5/6, 2015 at 9:11 Comment(4)
I like this answer and haven't had a chance to try it. the splitcontainer worked for me. Is there a benefit to doing it this way? I'm having resize issues where my width only doesn't work at all, you can't decrease height or width, the vertical arrows act like diagonal, and both the vertical and diagonal arrows increase the height/width in an odd scaleOdaodab
I may use this in case I have issues with the split containerOdaodab
Do I have to register the OnResize event anywhere?Odaodab
When you are overriding OnResise method of form you don't need additionally subscribe to OnResise event. In fact, all these subscribed handlers (if any) are called in base.OnResise method.Ostler

© 2022 - 2024 — McMap. All rights reserved.