Do some events for many things

Lanc1988

Contributor
Joined
Nov 27, 2003
Messages
508
I have about 20 labels on a form.

I have some more code that does two different things:
First, you will need to know that i have a textbox that goes along with each label and what is entered in that textbox is a number.

So, the code first decides if the number is less than 100, if it is then it just puts that number in the label and also if it is less than 100 it will put a number in ( ) after the number in the label depending on what that number was, example: if the number was 50 in the textbox, in the label it would put "50 (123456)"

The other thing the code will do is if the number is more than 100, if it is then it will do some code like if that number was between 1111 and 2222 then it would put a certain number in the label and in the ( ) put what was in the text box, example: if the number in the textbox was 123456, in the label it would put "50 (123456)"


Now my problem is, i can do all of what i need to do, but since i have around 20 different labels to do this for, it would be tons of code, so is there a way to somehow first use a code for one label, then like loop it and for the number label use the same code?
 
So lets see.

You have about 20 sets of label + textbox. Where the text entered in the textbox has to be processed, and then the text of the label is updated. For each of those 20 textbox/label combinations the processing is the same.

The quickest way would be to create a separate function called eh..
ProcessMyTextbox
that takes both a textbox and a label. It would do all the processing, so it would look like this;
Code:
public void ProcessMyTextbox(TextBox text, Label label)
{
//do some stuff and update the text / label
}
Now you only have to call that piece of code from every event handler.

It would already remove most of the duplicate code but if you want you can go one step further. During the load of the form, you set the tag property of each textbox to point to the matching label.

I dont know when your code is triggered, but I'll asume during either keypress or validation event of the textbox. Which doesnt really matter, as each textbox event also gives the original sender -> the textbox.

The trick is that you can create one generic event handler that can be used by all the textboxes.

What the event handler does is use the sender object and cast it base to the TextBox class (you now have the textbox), and get the Tag where you previously stored the associated label. With those two you can use the ProcessMyTextbox method.
In psuedo code that would look like:
Code:
private void myTextbox_KeyPressed(object sender, <some other argument i'm not interested in)
{
  TextBox textBox = sender as TextBox;
  //ok, got the sending textbox, now get the label
  Label labelToUpdate = textBox.Tag as Label;
  //now call our generic method
  ProcessMyTextBox (textBox, labelToUpdate);
}
All the textboxes can use that one event handler. That means you only coded 1 event handler and 1 method to do the actual processing (you could combine them if you want into 1 total method that does everything).
Saves copy and paste 19 times with all the associated problems that you get when you copy code where you might forget to update something and get the weirdest bugs.
 
Back
Top