views:

70

answers:

7

Forms

I have 2 input textboxes that take a host or IP. When the user leaves an input box an event is fired that checks the input to see if it is actually a live computer. The results are then put into the appropriate label.

My question is, should I be using separate events for each input box, since they update different labels? Or, can I use 1 event and check who the caller was, then update the appropriate label?

+3  A: 

As they update separate labels I'd go for two event handlers. It's cleaner and doesn't require any logic.

These can call a utility method that does the actual update if you want to keep that code in one place.

ChrisF
A: 

You can do it either way, but i would say that you should use 2 different event handlers then you don't need any logic to decide what to do based on the caller. It makes for cleaner, easier to understand code.

Ben Robinson
A: 

Use two events and have the handlers call a common method which takes the label as the parameter.

Bryan Watts
A: 

Maybe have one main event handler (not a real event handler, but similar) that accepts a reference to a label, and individual event handlers that call the main one, something like:

private void AnyTextBox_Leave(object sender, EventArgs e, Label labelToUpdate)
{
  // Do stuff
}

private void TextBox1_Leave(object sender, EventArgs e)
{
  AnyTextBox_Leave(sender, e, lbl1);
}
private void TextBox2_Leave(object sender, EventArgs e)
{
  AnyTextBox_Leave(sender, e, lbl2);
}
Joe Enos
A: 

I would register two separate events one for each label.

VoodooChild
+1  A: 

short answer yes.

If you just have two then you could write a switch statement. But then you would be tightly coupled to the actual labels.

The other option to to place the label in the Tag property, then when you get the sender get the tag of the sender, cast as a label and set the text.

Mike
+1  A: 

Another alternative is a custom control that contains the textbox and label, and build the event handler into the control's textbox_leave event, assuming you've got consistent logic for all controls.

Joe Enos