Reputation: 13
I'm programming in c#. I need a function which creates a Button, specifies its name and some of its events. I need to pass its name and events as arguments. I 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 another part of the code I make these calls:
createButton("cmdPrint", this.Hover, this.Print);
createButton("cmdMark", this.Hover, this.Mark);
The calls generate this error: "The best overloaded method match for createButton(string, System.EventHandler, System.EventHandler)' has some invalid arguments".
What type of arguments should hover and click be?
Edit:
Jon: Hover and Print are Events:
private void Hover(object sender, EventArgs e)
{
Proofs.ShowInformation((Control)sender);
}
private void Print(object sender, EventArgs e)
{
Proofs.Print((Control).sender);
}
The two call lines generate the same error.
Steve: My Events Print and Hover have the typical Event syntax, but I don't know what type should have hover and click in createButton function.
Edit2:
My problem is solved. I just add this delegate:
private delegate void Del(object sender, EventArgs e);
Change the calls:
Del print = this.Imprimir;
Del hover = this.Hover;
createButton("cmdPrint", this.Hover, this.Print);
createButton("cmdMark", this.Hover, this.Mark);
And change the arguments (in createButton):
private void createButton(string name, Del hover, Del click)
Thanks a lot.
Upvotes: 0
Views: 192
Reputation: 20620
The two EventHandler parameters should be functions that have this signature:
void MyFunction(Object sender, EventArgs e)
If you look up EventHandler
on the MSDN website, you can see the syntax of this delegate described as:
[SerializableAttribute]
[ComVisibleAttribute(true)]
public delegate void EventHandler(
Object sender,
EventArgs e
)
This tells you the return type and the type of the parameters for that signature.
Upvotes: 1