我希望能够重置为“0”的表单上有几个标签。我通过引用每个标签来解决它。我还使用了数组和 for 循环,这似乎更有效率。有没有办法通过使用 for 变量并构造一个字符串来引用每个标签名称,而不用数组来评估标签名称?
像这样:
for (int x = 0; x < 6; x++)
lbls[x].Text = "0";
这是我的代码:
// Clear form labels
private void btnClear_Click(object sender, EventArgs e)
{
Label[] lbls = new Label[]
{
lbl1, lbl2,lbl3,lbl4,lbl5,lbl6
};
for (int x = 0; x < 6; x++)
lbls[x].Text = "0";
//lbl1.Text = "0";
//lbl2.Text = "0";
//lbl3.Text = "0";
//lbl4.Text = "0";
//lbl5.Text = "0";
//lbl6.Text = "0";
}