write2mtg

Hi All,

I have a server button control. How to pass arguments in the onclick event of the button.

By default, the button takes two arguments, how to pass a third argument.

Murali



Re: Windows Forms General How to pass arguments to a server button control

timvw

You would have to define a delegate that takes 3 arguments, and define the Click event using that delegate

Code Snippet

delegate void MyEventHandler(object source, EventArgs e, MyExtra extra);
public event MyEventHandler Click;


I don't see a good reason why anyone would want to do it that way though.. I think you're better of using the standard EventHandler delegate and implement your own EventArgs class and then create an instance of this class in when you raise the event...

Code Snippet

class MyEventArgs : EventArgs {
private MyExtra extra;

public MyEventArgs(MyExtra extra) : base() {
this.extra = extra;
}

public MyExtra {
get { return this.extra; }
set { this.extra = value; }
}
}

private void RaiseClickEvent() {
if (Click != null) {
MyEventArgs args = new MyEventArgs(myExtraInstance)
Click(this, args);
}
}







Re: Windows Forms General How to pass arguments to a server button control

write2mtg

Hi,

Thank you very much. I think this will defintely solve my problem.

Murali





Re: Windows Forms General How to pass arguments to a server button control

Javed Ahmad

hi,

this is same problem with me. but i could not understand your code.

Have u complete code

or

can u define it by an example

thnx





Re: Windows Forms General How to pass arguments to a server button control

timvw

First you implement your own button, so that the right EventArgs are passed:

Code Snippet

public MyButton : Button
{
protected override OnClick(EventArgs e)
{
EventHandler handler = (EventHandler) base.Events[EventClick];
if (handler != null)
{
MyClickEventArgs myClickEventArgs = new MyClickEventArgs(e);
// set the extra data....
handler(this, myClickEventArgs);
}
}
}


And in the EventHandler you can cast the EventArgs to MyClickEventArgs

Code Snippet

public void button_Click(object sender, EventArgs e)
{
MyClickEventArgs myClickEventArgs = e as MyClickEventArgs;
if (myClickEventArgs != null)
{
// do something with the extra data....
}
}