Reputation: 2543
I would Like to use a function with parameters on the properties box with event list. this even list box on the right int he visual studio only allows some functions with object sender and eventargs e, and doesn't show a custom function with parameters. Is this a general behavior or should I look for an error?
Upvotes: 0
Views: 116
Reputation: 545
You can simply use the CommandArgument
of your button.
yourButton.Click += new EventHandler(yourButton_Click);
yourButton.CommandArgument = "your parameters";
and in the event handler method do the following:
string yourParameters = (sender as Button).CommandArgument.ToString()
Hope this helps.
Upvotes: 1
Reputation: 48975
The correct way of using events in .Net is the following:
EventHandler
sender
and e
. sender
must be of type object
and should contain a reference to the instance of the class that raised the event. e
must be of a type that inherits from EventArgs
(often EventArgs
itself). e
is the instance that contains the event data.You must not expect to find any other event handler signature, it's always (object sender, XXXEventArgs e)
.
Note that when you raise events and do not need to pass event data, EventArgs.Empty
should be used.
public delegate void MyEventHandler(object sender, EventArgs e);
public event MyEventHandler MyEvent;
public void OnMyEvent()
{
if (this.MyEvent != null)
{
this.MyEvent(this, EventArgs.Empty);
}
}
Upvotes: 0
Reputation: 5566
you can only register handlers on an event which correspond to the event declaration (delegate signature).
If you want to use predefined event and dont create your own you have to follow the event handler signature
Upvotes: 0
Reputation: 18843
Button.Click
is defined as it is, and there is no way to change it. However,
yourButton.Click += delegate { YourButton_click(1); }
will do the job.
Upvotes: 0
Reputation: 20320
Write a descendant of EventArgs.
e.g.
public class MyEventArgs: EventArgs {
public MyEventArgs(string arg1, int arg2) {
this.MyValue = arg1;
this.MyNumber = arg2;
}
public string MyValue;
public int MyNumber;
}
Nows it's an okay parameter for an EventHandler, when you write one e, will have MyValue and MyNumber properties.
Upvotes: 1