我如何按下控制
How do I push down control
我有一个带有可变长度文本的标签,下面有一个进度条。我想在该标签和 progressBar 之间保留一个 space,因此根据标签的文本(被包装)progressBar 应该被向下推,始终在它们之间保持 space。我该怎么做?我尝试了 AutoSize = true
和 AutoSizeMode = GrowAndShrink
但它没有改变任何东西。示例:
---------------------------
| 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 |
---------------------------
示例 2:
---------------------------
| small text |
| |
| progressBar here |
---------------------------
设置你的label.text。然后
progessBar.Top = label.Bottom + WhateverSpaceYouWant
如果您保存进度条的初始 Y
位置,您可以稍后根据标签的高度动态设置位置。它将保留您最初设置的填充。
因此,如果我有以下表单,其中标签在按下按钮时自动更新,我可以在按钮单击事件中更新进度条位置。
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);
}
}
点击几次按钮后得到以下结果:
如果您的标签以一些文本开头,您可能需要从新的 Y 中减去原始标签高度,否则第一次填充会略微增加。
将 Label 和 ProgressBar 放入 FlowLayoutPanel 中,其 FlowDirection
属性 设置为 TopDown
。现在,当 Label 垂直增长时,ProgressBar 将自动向下推。要控制Label和ProgressBar之间的距离,更改Label的Padding
属性中的Bottom
值。
这是我的表单在表单和 FlowLayoutPanel 上将 AutoSize
设置为 true
几次后的样子(在 AutoSizeMode
中使用 GrowOnly
):
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 ";
}
}
}
我有一个带有可变长度文本的标签,下面有一个进度条。我想在该标签和 progressBar 之间保留一个 space,因此根据标签的文本(被包装)progressBar 应该被向下推,始终在它们之间保持 space。我该怎么做?我尝试了 AutoSize = true
和 AutoSizeMode = GrowAndShrink
但它没有改变任何东西。示例:
---------------------------
| 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 |
---------------------------
示例 2:
---------------------------
| small text |
| |
| progressBar here |
---------------------------
设置你的label.text。然后
progessBar.Top = label.Bottom + WhateverSpaceYouWant
如果您保存进度条的初始 Y
位置,您可以稍后根据标签的高度动态设置位置。它将保留您最初设置的填充。
因此,如果我有以下表单,其中标签在按下按钮时自动更新,我可以在按钮单击事件中更新进度条位置。
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);
}
}
点击几次按钮后得到以下结果:
如果您的标签以一些文本开头,您可能需要从新的 Y 中减去原始标签高度,否则第一次填充会略微增加。
将 Label 和 ProgressBar 放入 FlowLayoutPanel 中,其 FlowDirection
属性 设置为 TopDown
。现在,当 Label 垂直增长时,ProgressBar 将自动向下推。要控制Label和ProgressBar之间的距离,更改Label的Padding
属性中的Bottom
值。
这是我的表单在表单和 FlowLayoutPanel 上将 AutoSize
设置为 true
几次后的样子(在 AutoSizeMode
中使用 GrowOnly
):
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 ";
}
}
}