C# - Generic Event Handler

I have some code pattern that goes like

   private void Button1_Click(object sender, EventArgs e)
            {
                Label1.Text = Textbox1.Text;
            }

	    private void Button2_Click(object sender, EventArgs e)
            {
                Label2.Text = Textbox2.Text;
            }

            and so on around 15  of this kind ..

Is there a way to handle this using generic <T> event instead of writing 15 of button click events.
Any help / suggestions on this are appreciated . Thanks in advance.

Well, you can create one method without using generic <T>

Create 1 method like this:
private void Button_Click(object sender, EventArgs e)
{

        }

Then link all your buttons to this method. Then in that method, you can get a reference to the calling button: Button btn = (Button)sender;

Then you can use that to identifiy which button was clicked. Be it by ID, Text, or what ever else you might need to check.

Hope that helps

You got a good one there but there I’d need to write a lot of if else / Switch Cases in the event .
something like


if (btn.Id == "Button1"  )
{

	Label1.Text = Textbox1.Text;
}
else if (btn.Id == "Button2"  )
{

	 Label2.Text = Textbox2.Text;
}

I was hoping for something like this if its possible



Button1.Click + = new  EventHandler(ClickEvent, Label1, Textbox1)
Button2.Click + = new  EventHandler(ClickEvent, Label2, Textbox2)




private Event_Click(object sender, EventArgs e,  control1 ,control2 )
{
	   (label)control1.Text = (Textbox1)Control2.Text;
}


I know the above is not correct code , but I hope you get the Idea .

Never mind , I got the solution . works for now


Button1.Click += delegate(object sender, EventArgs args) { Button_Click(Label1, Textbox1); };


  public void Button_Click<T, U>(T Control1, U Control2)
        {
            Label L1 = Control1 as Label;
            TextBox T1 = Control2 as TextBox;

            L1.Text = T1.Text;

        }


I’d still like to improve on this as i need to explicitly convert Control 1 & 2 to their types . I know I could have simply put in the data types for input parameters instead of generic T & U.

Thanks,

You are thinking about this wrong – the way to handle it isn’t some sort of generic event handler, especially considering your senders (buttons) don’t seem to be involved, just some magically named text boxes and labels. You need to establish a relationship here.

Personally, I would establish the relationship using a usercontrol, which could handle the event internally. Then you’d just need 15 of those on the page and life would be good.

I’m not sure what you mean there but buttons not being involved & magically named text boxes & labels. Their names could be anything & the ids just need be passed as arguments to the delegate. Also the buttons are involved aren’t they ?
Id like to know if you mean something else.

You mean have 15 user controls ? That’s good as well, But I don’t understand the part of establishing a relation using the usercontrol.

I said magic relationships because that isn’t the way .NET events work. Or at least ones you don’t build yourself. The delegate event handler method gets 2 parameters – and you’ve got zero control over what they are in this case. Which means passing in data has to happen over one of those two vectors or not at all. Now, the obvious thing to look at would be the EventArgs, but that is pretty useless in this case – there isn’t any data attached, it is just there to satisfy the pattern. The other parameter is probably more interesting – it is the object originating the event. In this case your button. So you could use some little-used properties of the button – .CommandName and .CommandArg – to pass the data you want in. From there you could probably get to the rather ugly method of using FindControl to get your textbox and then figuring out what you wanted to do with it.

You mean have 15 user controls ? That’s good as well, But I don’t understand the part of establishing a relation using the usercontrol.

Pretty simple really. UserControl is self-contained, and your button knows it should update it’s own text box. Since it imlements INamingContainer, you can stack the user controls on the page and not have them crosstalk. It is basically like having a single button/single textbox.