I have a bunch of text boxes in a panel on my C# Winform. Each row of text boxes are named like this:
tb1 tbNickName1 comboBox1
tb2 tbNickName2 comboBox2
tb3 tbNickName3 comboBox3
and so on.
I have a button next to each of the rows of text boxes. But instead of having the button point to a different event for each button, I want to point the button to just the button1_Click event and have it do all of the processing there. I know how to do this and all my buttons point to the button1_Click event.
But I need to be able to determine which button it was called from (which I am able to do), but I need to manipulate the name of the text boxes in the event so I can do the processing based on what row I am in/button that I am calling from.
For example, if I am in row number 2 where the tb2 tbNickName2 comboBox2 textboxes are, then I need to be able to have the button1_Click event know this and automatically assign the tb2 tbNickName2 comboBox2 values to the tmp variables that I use in the example below.
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?
}
I know that I have not explained this very well, but it is the best I can do.
Button button = sender as Button;
string buttonIndex = button.Name.Substring(6);
string tempEmail = (this.Controls["tb" + buttonIndex] as TextBox).Text;
string tmpNickName = (this.Controls["tbNickName" + buttonIndex] as TextBox).Text;
string tmpGroup = (this.Controls["comboBox" + buttonIndex] as ComboBox).Text;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With