开发者

2 Controls, 1 event

Forms http://img101.imageshack.us/img101/6970/stackoverflowquestion.png

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 co开发者_JAVA百科mputer. 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?


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.


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.


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.


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.


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


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);
}


I would register two separate events one for each label.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜