<分区>
Possible Duplicate:
Find a control in C# winforms by name
假设我们有 4 个文本框(和一个按钮):
textBox1:( Here we must enter the name of the textBox where we want to change background)
textBox2:()
textBox3:()
textBox4:()
在我们的第一个文本框中,我们输入任何其他文本框的名称,当我们点击一个按钮时 - 背景会相应地改变。
通常我会这样做:
private void button1_Click(object sender, EventArgs e)
{
string variableName = textBox1.Text();
if (variableName == "textBox1")
{
textBox1.BackColor = Color.Black;
}
else if (variableName == "textBox2")
{
textBox2.BackColor = Color.Black;
}
else if (variableName == "textBox3")
{
textBox3.BackColor = Color.Black;
}
else if (variableName == "textBox4")
{
textBox4.BackColor = Color.Black;
}
}
另一种方法 - 更简单的方法是执行相同的操作:
private void button1_Click(object sender, EventArgs e)
{
string variableName = textBox1.Text();
variableName.BackColor = Color.Black;
}
仅此而已!所以我的问题是:
是否可以将字符串转换为示例中所示的“控件名称”?