In my program it is possible to draw a LinkLabel with the text which is defined before. When the LinkLabel is visible, I want to open a new form when I double click the LinkLabel
this is the code I have so far:
private Graphics g;
private int punt1 = 0;
private int punt2 = 0;
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
int x = e.X;
int y = e.Y;
g = this.CreateGraphics();
if (e.X > 20 && e.Y > 100)
{
...
if (rbTekst.Checked && MousePosition.X > 1000)
{
string naam = tbUseCaseName.Text;
LinkLabel lb = new LinkLabel();
UseCase uc = new UseCase();
lb.MouseDoubleClick += uc.Show();
lb.Location = e.Location;
lb.Text = naam;
this.Controls.Add(lb);
}
Everything works great except the DoubleClick event. I get the following error message:
Cannot implicitly convert type 'void' to 'System.Windows.Forms.MouseEventHandler'
Does this error occurs because Form1_MouseUp has MouseEventsArgs e?
Thanks in advance!
You are trying to assign the result of calling uc.Show();
as the event handler for the MouseDoubleClick
event. That method returns void
which results in the error message you see.
If the signature** of UseCase.Show
matches the expected signature of the event handler you can just assign the method itself:
lb.MouseDoubleClick += uc.Show;
Given your comments though it would appear that the signature doesn't match. In that case you could create an anonymous event handler that simply calls the method you want to call:
lb.MouseDoubleClick += (sender, e) => uc.Show();
Personally I prefer to enclose the method body in curly braces but it's not required:
lb.MouseDoubleClick += (sender, e) => { uc.Show(); };
You could also create a method to handle the click and call show from within there:
lb.MouseDoubleClick += lb_MouseDoubleClick;
...
void lb_MouseDoubleClick(object sender, MouseEventArgs e)
{
uc.Show();
}
** The signature of a method is what the method looks like in terms of access modifiers, the return value and any parameters it accepts. I guess that uc
is a Form
in which case the Show
method returns void
and accepts no parameters. The MouseDoubleClick
event expects a method that takes an object
and a MouseEventArgs
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments