我的 C# Winform 面板中有一堆文本框。每行文本框的命名如下:
tb1 tbNickName1 组合框
1 tb2 tbNickName2 组合框2
tb3 tbNickName3 组合框3
等等。
我在每一行文本框旁边都有一个按钮。但不是让按钮指向每个按钮的不同事件,我想将按钮指向 button1_Click 事件并让它在那里完成所有处理。我知道怎么做,我所有的按钮都指向 button1_Click 事件。
但是我需要能够确定从哪个按钮调用它(我可以这样做),但是我需要操纵事件中文本框的名称,以便我可以根据我所在的行进行处理/我从中调用的按钮。
例如,如果我在 tb2 tbNickName2 comboBox2 文本框所在的第 2 行,那么我需要能够让 button1_Click 事件知道这一点并自动将 tb2 tbNickName2 comboBox2 值分配给我在下面的示例中使用的 tmp 变量.
private void button1_Click(object sender, EventArgs e)
{
Button bt = (Button) sender; //will return 'button1'
string tmpEmail = null;
string tmpNickName = null;
string tmpGroup = null;
//I don't want to hard code the tb1.Text value here, I want to have
// the namechange based on which (Button) sender it was called from.
// For example button1 should assign all the
// tb1 tbNickName1 comboBox1 values
//If called from button2, then it should assign the
//tb2 tbNickName2 comboBox2 values instead
//How can I do this so tb1.Text is based off of the button # that I am
//calling for example tb(bt).Text would be cool but that does not work.
tmpEmail = tb1.Text; //What do I change tb1.Text to based on button #?
tmpNickName = tbNickName1.Text; //What do I change tbNickName1.Text to?
tmpGroup = comboBox1.Text;//What do I change comboBox1.Text to?
}
我知道我没有很好地解释这一点,但这是我能做的最好的。