简体   繁体   English

调整相邻控件的大小时调整 TableLayoutPanel 的大小

[英]Resize TableLayoutPanel when adjacent control is resized

A WinForms application. WinForms 应用程序。 One of the forms consists of a MenuStrip , a custom control LabelProgressBar below that, and a TableLayoutPanel below that.其中一个表单由一个MenuStrip 、一个自定义控件LabelProgressBar和下面的TableLayoutPanel

While the program is running, the size of the LabelProgressBar is changed.在程序运行时, LabelProgressBar的大小会发生变化。 The TableLayoutPanel should expand or contract as it is resized. TableLayoutPanel应在调整大小时扩展或收缩。 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 .因此,如果LabelProgressBar的高度减小到零,则它看起来就好像TableLayoutPanel及其内容直接位于MenuStrip下方。

在此处输入图片说明

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.因此,为了适当的控制,已经对各种船坞和锚的安排进行了大量尝试,但没有一个导致所需的行为。

This works perfectly with two panels - one for the top, with DockStyle.Top , and the "main" one with DockStyle.Fill .这与两个面板完美配合 - 一个用于顶部,带有DockStyle.Top ,“主要”一个带有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.您可以尝试将自定义控件包装在面板中并尝试锚定或Fill ,如果它没有正确停靠到顶部。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM