Saturday, 15 February 2014

c# - void as an argument -



c# - void as an argument -

i'm programming in c#. need function creates button, specifies name , of events. need pass name , events arguments. have done this:

private void createbutton(string name, eventhandler hover, eventhandler click) { button button = new button(); button.name = name; button.image = properties.resources.print_trans; button.mousehover += new system.eventhandler(hover); button.click += new system.eventhandler(click); button.visible = false; this.controls.add(button); }

in part of code create these calls:

createbutton("cmdprint", this.hover, this.print); createbutton("cmdmark", this.hover, this.mark);

the calls generate error: "the best overloaded method match createbutton(string, system.eventhandler, system.eventhandler)' has invalid arguments".

what type of arguments should hover , click be?

edit:

jon: hover , print events:

private void hover(object sender, eventargs e) { proofs.showinformation((control)sender); } private void print(object sender, eventargs e) { proofs.print((control).sender); }

the 2 phone call lines generate same error.

steve: events print , hover have typical event syntax, don't know type should have hover , click in createbutton function.

edit2:

my problem solved. add together delegate:

private delegate void del(object sender, eventargs e);

change calls:

del print = this.imprimir; del hover = this.hover; createbutton("cmdprint", this.hover, this.print); createbutton("cmdmark", this.hover, this.mark);

and alter arguments (in createbutton):

private void createbutton(string name, del hover, del click)

thanks lot.

the 2 eventhandler parameters should functions have signature:

void myfunction(object sender, eventargs e)

if eventhandler on msdn website, can see syntax of delegate described as:

[serializableattribute] [comvisibleattribute(true)] public delegate void eventhandler( object sender, eventargs e )

this tells homecoming type , type of parameters signature.

c# delegates argument-passing

No comments:

Post a Comment