基本上,按钮的标记属性是我需要动态引用的现有组合框的名称。这是处理多个按钮的通用函数。帮助
private void SQLButton(object sender, EventArgs e)
{
magic(((Button)sender).Tag.ToString());
}
private void magic(string currentcombo)
{
string CurrentText = (ComboBox).(currentcombo).Text;
}
最佳答案
您可以将 Tag 属性设置为实际的 ComboBox 并完全避免您的问题。
//code when defining your button...
{
sqlButton.Tag = comboBoxA; //instead of comboBoxA.Name
}
private void SQLButton(object sender, EventArgs e)
{
Button button = sender as Button;
ComboBox comboBox = button.Tag as ComboBox;
if (comboBox == null )
{...}
else
{
magic(comboBox);
}
}
private void magic(ComboBox currentcombo)
{
string CurrentText = currentcombo.Text;
}
关于C#:将字符串转换为对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1380181/