Hey Everyone,

I'm trying to have a form autosize when a user clicks on a Link Label. When the Link Label is clicked, I want the form to expand slightly, and show a tab control that I've created. I can't figure out how to use the this.autosize feature correctly for this though. Can someone give me a hand? Thanks

Recommended Answers

All 9 Replies

Here's a small example. One form with two buttons and few other controls. Button1 "initializes" AutoSize property. Button2 toggles controls on and off, except Button controls.

private void button1_Click(object sender, EventArgs e)
{
  this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
  // this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly; 
  this.AutoSize = true;
}

private void button2_Click(object sender, EventArgs e)
{
  // Toggle controls on/off -> form grows/shrinks
  listView1.Visible = !listView1.Visible;
  comboBox1.Visible = !comboBox1.Visible;
  textBox1.Visible = !textBox1.Visible;
}

See the attached images how the form changes.

In AutoSizeMode.GrowAndShrink mode you can't manually resize form, only form's maximize and minimize controls work.

HTH

In addition to Teme64's quick response (faster than I was anyway:D) you can manage the space around the controls by adjusting the forms Padding property or each control's Margin property.

Hmm I can't get mine to work - here's what I'm doing (just for testing right now):

private void Form_Vehicle_Checkin_Load(object sender, EventArgs e)
        {
            this.AutoSize = true;
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            tabControl1.Visible = false;
        }

When it loads I just get blank space on my form.

That is because you have the VIN: control below the TabControl.
Try adding both the VIN: control and TabControl in to a FlowLayoutPanel.

Edit: Set the FlowLayoutPanel AutoSize=true, AutoSizeMode.GrowAndShrink, FlowDirection=TopDown.

Excellent. Thanks. So how do I use that padding control?

Welp, it's not working quite right. I added this:

private void linkLabel_Expense_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            tabControl_Expenses.Visible = true;
        }

And it doesn't do anything.

edit> Also, How can I keep controls at the bottom of my form and have them move when the tab_control is visible? E.g., I need to have an OK, and Cancel button at the bottom. How can I make them "float" as the main form grows and shrinks?

I need to have an OK, and Cancel button at the bottom. How can I make them "float" as the main form grows and shrinks?

OK. Try this.
Drop the OK and Cancel buttons (and anything else you need below the TabControl) in to a Panel and put this after the TabControl in the FlowLayoutPanel.
The FlowLayoutPanel now has two items in it (a TabControl and a Panel).
If FlowLayoutPanel.FlowDirection = TopDown then the TabControl should be above the Panel.
When the TabControl.Visible = false the FlowLayoutPanel pulls the Panel to the top and resize, this in turn causes the form to resize.

When the TabControl.Visible = true the FlowLayoutPanel pushes the Panel to the bottom to make room for the TabControl and resize, this in turn causes the form to resize.

Excellent. Thanks. So how do I use that padding control?

The Margin property is used by the designer to stop controls bumping in to each other when snapping. It is also used by Forms during AutoSize to stop the edges from getting too close to controls and by FlowLayoutPanels to stop the controls from getting too close to each other.

The Padding property is used by container controls (such as Forms and Panels) to keep the inside edge of the container free.

Welp, it's not working quite right.

If you are still having trouble post your code and if possible, the form/panel settings.

There is an alternative to using AutoSize.

Set the Anchor property of controls at the bottom of the form to anchor at Bottom/Left instead of Top/Left then, as well as hiding the TabContol, manually resize the form to a known fixed size. The controls anchored at the bottom will move up/down as the form shriks/grows.

OK, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.