我在C#Windows窗体中有一个表单,每次单击一个按钮我都会更改索引,并且我想根据此索引更改Label的内容。我找到的唯一选择是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--;
}
答案 0 :(得分:3)
尝试以下
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";