How do I push down control

2019-07-21 15:03发布

问题:

I have a label with variable-length text and a progressBar below to it. I want to keep a space between that label and the progressBar, so depending to label's text(which is wrapped) the progressBar should be pushed down, always keeping the space between them. How should I do that? I tried AutoSize = true and AutoSizeMode = GrowAndShrink but it didn't changed anything. Example:

 ---------------------------
|  for example the label's  |
|  text might be something  |
|  like this, with a lot of |
|  of text but the progress |
|  bar should be here       |
|                           |
| progressBar here          |
 ---------------------------

example 2:

 ---------------------------
|  small text               |
|                           |
| progressBar here          |
 ---------------------------

回答1:

Put the Label and the ProgressBar into a FlowLayoutPanel that has its FlowDirection property set to TopDown. Now when the Label grows vertically the ProgressBar will be pushed down automatically. To control the distance between the Label and the ProgressBar, change the Bottom value in the Padding property of the Label.

Here's what my form looks like after hitting the button a couple times with AutoSize set to true on the Form and the FlowLayoutPanel (using GrowOnly in AutoSizeMode):

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        for(int i = 1 ; i < 20; i++)
        {
            label1.Text = label1.Text + " more ";
        }
    }
}


回答2:

If you save the initial Y position of your progressbar, you can set dynamically the location later according to your label's height. It will keep the padding you've set originally.

So if I have the following form, where the label is updated automatically on the button press, I can update the progress bar location in the button click event.

public partial class Form1 : Form
{
    private readonly int initialProgressbarLocationY;

    public Form1()
    {
        InitializeComponent();

        label1.MaximumSize = new Size(80, 1000); //Wrapping label
        label1.AutoSize = true;

        initialProgressbarLocationY = progressBar1.Location.Y; //Save the original position
    }

    private void button1_Click(object sender, EventArgs e)
    {
        label1.Text += "bla blablablabla bla";
        MoveProgressbar();
    }

    private void MoveProgressbar()
    {
        // Set the progressbar at the same X, but update the Y according to the label's height
        progressBar1.Location = new Point(progressBar1.Location.X,
            initialProgressbarLocationY + label1.Height);
    }
}

Which gives the following result after a few button clicks:

If your label starts with some text, you may need to subtract the original label height from the new Y or the padding will slightly increase the first time.



回答3:

Set your label.text. then

progessBar.Top = label.Bottom + WhateverSpaceYouWant