自动更改 C# 窗体中的标签
本文关键字:标签 窗体 | 更新日期: 2023-09-27 18:31:16
>我在 C# Windows 窗体中有一个窗体,每次单击按钮时,我都会更改索引,并且我想基于此索引更改标签的内容。我找到的唯一选择是label_click但是我希望更改是自动的。对此有任何想法吗?
// lines a gloab list of strings and index changes from a button click
private void label1_Click(object sender, EventArgs e)
{
label1.Text = "videos/" + lines[index] + ".mp4";
}
private void button4_Click(object sender, EventArgs e)
{
index++;
}
private void button3_Click(object sender, EventArgs e)
{
if (index >= 1)
index--;
}
尝试以下操作
List<string> lines = new List<string>(){/*initialization here*/}
int index = 0;
private void button4_Click(object sender, EventArgs e)
{
//Ensure index is inside List bounds.
index = Math.Min(lines.length -1 , index + 1);
ChangeLabelText()
}
private void button3_Click(object sender, EventArgs e)
{
//Ensure index is inside List bounds.
index = Math.Max(0 , index - 1);
ChangeLabelText()
}
void ChangeLabelText() => label1.Text = "videos/" + lines[index] + ".mp4";