Warm tip: This article is reproduced from stackoverflow.com, please click
.net c# user-interface winforms

Resize TableLayoutPanel when adjacent control is resized

发布于 2020-06-01 13:12:09

A WinForms application. One of the forms consists of a MenuStrip, a custom control LabelProgressBar below that, and a TableLayoutPanel below that.

While the program is running, the size of the LabelProgressBar is changed. The TableLayoutPanel should expand or contract as it is resized. So, if the height of the LabelProgressBar is reduced to zero, it should look as if the TableLayoutPanel and its contents are directly below the MenuStrip.

enter image description here

A screenshot can be included if it would be helpful.

So, far attempts have been made with various dock and anchor arrangements for the appropriate controls, and none resulted in the required behaviour.

Questioner
Al2110
Viewed
11
gnud 2020-03-18 07:15

This works perfectly with two panels - one for the top, with DockStyle.Top, and the "main" one with DockStyle.Fill.

You could try wrapping your custom control in a panel and experiment with anchoring or Fill'ing, if it doesn't dock properly to the top.

var form = new Form();

var shrinking = new Panel() 
{
    BackColor = Color.Red,
    Dock = DockStyle.Top
};
var filling = new TableLayoutPanel()
{
    BackColor = Color.Green,
    Dock = DockStyle.Fill
};

var timer = new System.Windows.Forms.Timer();
timer.Interval = 500;
timer.Tick += (s, a) =>
{
    shrinking.Height -= 10;
    if(shrinking.Height <= 0) {
        shrinking.Height = 0;
        timer.Stop();
    }
};

form.Shown += (s, a) => timer.Start();


// Just to make sure it works with a menu present   
var menu = new MenuStrip();
menu.Items.Add("&File");    

form.Controls.Add(shrinking);   
form.Controls.Add(filling); 
form.Controls.Add(menu);
form.ShowDialog();