Reputation: 3221
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.
Upvotes: 5
Views: 3709
Reputation: 6102
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;
Upvotes: 1
Reputation: 12613
First off, for your own, ours, and anyone else ever looking at your codes sake, change your naming convention.
As for a solution, I would suggest putting references to your checkboxes and text boxes into a dictionary when you construct them. Make the buttons contain the key you used in their CommandArgument
. Now you can retreive the row by accessing the dictionary.
For instance, in your constructor, you'd add something like this
_rowDictionary = new Dictionary<string, Row> {
{ "identifier of first line", new Row { descriptiveNameOfFirstTextBox, descriptiveNameOfFirstTextBox, descriptiveNameOfButton } },
// ...
};
descriptiveNameOfButton.CommandArgument = "identifier of first line";
Then, in your event handler
Button senderButton = (Button) sender;
Row r = _rowDictionary[senderButton.CommandArgument];
Here, I've used some container class Row
. You may of may not need it
class Row {
TextBox textBoxForInputOfA { get; set; }
TextBox textBoxForInputOfB { get; set; }
Button buttonForDoingWhatItDoes { get; set; }
}
Upvotes: -1
Reputation: 72696
You can do something like this (iterating over the controls in the panel) :
//Get the number of the button control (last digit of the name)
string strNum = bt.Name.Substring(bt.Name.Lenght -2);
foreach(Control ctrl in myPanel.Controls)
{
if(ctrl is ComboBox) {
if(ctrl.Name.EndsWith(strNum)) {
//Do Something with your found ComboBox ...
}
}
}
Code not tested but should give you an idea ...
Upvotes: 1
Reputation: 1835
On the buttons, you add the CommandArgument-Property which could look something like this
bt1.CommandArgument = "1";
bt2.CommandArgument = "2";
In your EventHandler, you read the CommandArgument and act accordingly
private void button1_Click(object sender, EventArgs e)
{
Button bt = (Button) sender;
if(bt.CommandArgument == "1") {
// bt1 was clicked, handle stuff accordingly
} else if(bt.CommandArgument == "2") {
// bt2 was clicked..
} else {
//handle rest of possible cases
}
}
It's only pseudo code but I think you can see where this is going. However keep in mind, that your variable-naming is far from optimal. Chose names for your variables and controls that give you any kind of information instead of just naming them with numbers. That would help you a lot if you take a look at the code at a later time and try to understand it. Just like this, try to find meaningful CommanArgument-values, too.
Upvotes: 0