0

I have a form with a panel, the panel has a textbox and two buttons

This is the size I want the form to be.

enter image description here

But if I do this.Size = pnl.Size;

Then it comes too small

enter image description here

Here is a programmatically generated example on form load

Panel pnl = new Panel();
pnl.Size=new Size(200, 100);
pnl.Location = new Point(0, 0);
Button b1 = new Button();
Button b2 = new Button();
TextBox t1 = new TextBox();
b1.Text = "OK";
b1.Location = new Point(3, 58);
b1.Size = new Size(75, 23);
b2.Text = "Cancel";
b2.Location = new Point(103, 58);
b2.Size = new Size(75, 23);
t1.Location = new Point(0, 0);
t1.Size = new Size(200, 50);
t1.Multiline = true;
pnl.Controls.Add(b1);
pnl.Controls.Add(b2);
pnl.Controls.Add(t1);
this.Controls.Add(pnl);

this.Size = pnl.Size;

That last line, this.Size = pnl.Size isn't making the form big enough. How can I get the form to fit properly like in the first picture? (preferably without having to do lots of arithmetic and without manually figuring it out graphically then inserting the number in)

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
barlop
  • 12,887
  • 8
  • 80
  • 109
  • 3
    Try setting [Form.ClientSize](https://msdn.microsoft.com/en-us/library/9278sfx2(v=vs.110).aspx) instead. – Sinatr Jul 05 '16 at 13:10
  • You can Set `AutoSize` property of `Form` to `true` and Set `AutoSizeMode` to `GrowAndShrink`. You may find this post helpful: [How to automatically size a Window to fit its content](http://stackoverflow.com/a/32992721/3110834). Probably it's a better option. – Reza Aghaei Jul 05 '16 at 19:54
  • @RezaAghaei maybe though technically I have multiple panels in the form and I don't want it to shrink around all the contents of the form. – barlop Jul 05 '16 at 20:06
  • If you take a look at linked answer you can see a screenshot of the result. Also you can simply test it. Take a look at [Form.AutoSize](https://msdn.microsoft.com/en-us/library/system.windows.forms.form.autosize(v=vs.110).aspx) property, You can use `AutoSize` to force a form to resize to fit its contents. Isn't it what you are looking for? – Reza Aghaei Jul 05 '16 at 20:20
  • Also it seems current accepted answer is also talking about a single panel like you asked in the question *I have a form with a panel*. Did I miss something? – Reza Aghaei Jul 05 '16 at 20:22
  • @RezaAghaei Well, I didn't say in the question that the form had a single panel, I just said to fit the form aroudn *a panel*. The reason why I want that, is that I am changing the layout of the form, I have different panels for different layouts. I make one panel visible for one layout, and another panel visible for another layout. The accepted answer is flexible enough that it works for that situation, i.e. fitting the form around a(as asked, not 'the') panel, and not around the entire contents of a form. – barlop Jul 05 '16 at 22:37
  • @barlop I just try to help and the one who should be satisfied by the solutions is you. If you are happy now, it's enough for now. You know the requirements better :) – Reza Aghaei Jul 05 '16 at 22:39
  • By the way, invisible items has not any impact on `AutoSize`, so the `AutoSize` approach will work too. Just just keep [that](http://stackoverflow.com/a/32992721/3110834) solution in mind, maybe someday find it helpful :) – Reza Aghaei Jul 05 '16 at 22:41

1 Answers1

1

You can change your code to:

this.ClientSize = pnl.Size;