質問

I have two GroupBoxes on the left side of one of my TabControls, call them GroupBox A (top left), and GroupBox B (bottom left). The GroupBoxes do not resize like I would hope.

Example: When I resize the main form that has the TabControls with my mouse, or maximize it, or move it to a screen with lower resolution, GroupBox B keeps its width / height. This causes GroupBox B to draw over GroupBox A, kind of like a 'always on top' effect.

Desired: Would like both GroupBoxes to resize according to one another / proporitionally and fit the area they are given.

Ideas?

役に立ちましたか?

解決

A tableLayoutPanel can help with this.

  1. Add a tableLayoutPanel and size it to fit your needs.
  2. Anchor the table to Top, Bottom, Left, and Right
  3. Put GroupBoxA into the upper left cell of the table
  4. Put GroupBoxB into the lower right cell of the table
  5. For both GroupBox size them accordingly and anchor them to all 4 sides.
  6. Now they will grow and shrink proportionately with the app.

Additionally you can add more of your controls to the table. If you need a control to span multiple rows or columns use the RowSpan/ColumnSpan property.

他のヒント

If your GroupBox is inside another control like a tab or something, then do as below:

In my case I had a GroupBox inside a tab and I called the below methods in the InitializeComponent() method to force the Groupbox to adjust to the tab size.

this.groupBox4.ResumeLayout(false);
this.groupBox4.PerformLayout();
this.tabPage2.ResumeLayout(false);
this.tabPage2.PerformLayout();

And If you have multiple GroupBoxes, you need set the anchoring accordingly.

I faced a similar problem, I just had used the split container for both GroupBoxes, anchored the split container at top, bottom, left an right of my main form, and both GroupBoxes too, at top, bottom, left and right of their containers.

Slightly off topic from the original question, but my issue was that my MaximumSize field of my Group Box was not set, or was too small so I could not resize the height!

Altought the question is pretty old, someone might still find it useful... I had the same problem and found a working solution -> instead of changing the width/height of the GroupBox, change its minimum width/height in the Form's resize method

edit: fixed typo

    private void Form1_Resize(object sender, EventArgs e) {
        groupBox1.MinimumSize = new Size(this.Width /2, this.Height);
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top