Nagaraj Tantri
Nagaraj Tantri

Reputation: 5232

Button EventHandler in form

The button which we can create on the form is written in terms of event handler in Form1.Designer.cs as

this.button1.Click += new System.EventHandler(this.button1_Click);

Here Click is public event EventHandler 's type and this EventHandler is a delegate as

public delegate void EventHandler(object sender, EventArgs e);

Now,

why can't it be '='(equals)

this.button1.Click = new System.EventHandler(this.button1_Click);

and also when I am passing the argument this.button1_Click, how does it match up to void EventHandler(object sender, EventArgs e); delegate ? As here I have two arguments.

Please clear me with this.

Thank you

Upvotes: 2

Views: 5372

Answers (5)

igofed
igofed

Reputation: 1442

ttSo, let's see what event is.

Code, you are write

public event EventHandler MyEvent;

will compile to

private EventHandler MyEvent = null;

[MethodImp(MethodImplOptions.Synchronized)]
public void add_MyEvent(EventHandler value) {
    MyEvent = (EventHandler)Delegate.Combine(MyEvent, value);
}

[MethodImp(MethodImplOptions.Synchronized)]
public void remove_MyEvent(EventHandler<NewMailEventArgs> value) {
    MyEvent = (EventHandler)Delegate.Remove(MyEvent, value);
}

So, as you see, you cannot directly access to delegate and can only call += and -=, which is overridden for event class. Also you can manually manage this mechanism by overriding methods += and -=. You can do it like this:

public event EventHandler MyEvent
{
   add { //your code for += here }
   remove { //your code for -= here }
}

More about event and delegates you can read in book "CLR via C#". I found all of this in this book.

Upvotes: 1

JKhuang
JKhuang

Reputation: 1553

Dear Nagaraj Tantri,

For question 1: As said above, Due to Delegate can set up multi-event.

For question 2:As culithay said, if you want to pass custom arguments throug event buttion

click, if you want to use EventHandler and pass cutom own argument

you have to custom your control class and custom own event argument,

the custom event parameter CustomEventArg should inherit EventArg class.

You can take the sample code as below.

// Customs ColorChanged's event parameter.     
public class ColorChangedEventArgs : EventArgs
{
    private Color color;
    public ColorChangedEventArgs(Color c)
    {
        color = c;
    }

    public Color GetColor
    {
        get { return color; }
    }
}


    //Add this method in your custom control
    protected void ibtnTest_Click(object sender, ColorChangedEventArgs e)
    {
        //TODO;
    }

You can also referen MSDN here

Upvotes: 0

P.K
P.K

Reputation: 19127

What if you want to have multiple methods called on Click event. What you are doing with

this.button1.Click += new System.EventHandler(this.button1_Click);

is registering for this.button1_Click method to be invoked when Click event is raised. += adds handler and NOT assigns handler.

Upvotes: 1

Dave Rael
Dave Rael

Reputation: 1759

esentially, you are adding a handler to the event, not setting the one handler. you might want to have more handlers for an event. one handler should not preclude having other handlers because there might be multiple actions that you could want to take place in response to a single event that might happen in different classes and in different places and on different threads and under different conditions. += says make me a subscriber to this event (and potentially one subscriber among many).

Upvotes: 1

culithay
culithay

Reputation: 305

1/ it can not be '='(equals) because delegate is like a function pointer 2/ If you want to pass parameter to event button click, you have to make your own button class and implement Click event and have you own EventArgs

sample code:

public class MyEventArg
{
    int _param1;
    string _param2;
    //you can add more param
    public MyEventArg(int _param1,string _param2)
    {
        this._param1 = _param1;
        this._param2 = _param2;
    }
}

public delegate void MyButtonClickHandler(object sender, MyEventArg e)
public class MyButton:Control
{
     public event MyButtonClickHandler OnMyClick;
     //You can raise your event here
 protected override void OnClick(EventArgs e)
 {
           MyEventArg e = new MyEventArg(1,"a");//just sample data here
           this.OnMyClick(this,e);
     }
}

In the form that contains MyButton class instant

public partial class Form1 : Form
{
    MyButton myButton = new MyButton();
    public Form1()
    {
        InitializeComponent();
        myButton.OnMyClick += new MyButtonClickHandler(this.myButton_OnMyClicked);
    }

    private void myButton_OnMyClicked(object sender, MyEventArg e){
    //your implementation        
    }
}

Upvotes: 0

Related Questions