no overload for matches delegate 'system.eventhandler' MouseDown MouseUp MouseMove

negu picture negu · Apr 17, 2013 · Viewed 10k times · Source

I have to create labels in a form depending on a string[] called "descripcio". Each parameter of "descripcio" corresponds to a new label. Each label needs three events, MouseDown, MouseUp and MouseMove. The same events for all labels. The code is:

public FormEquip(string[] descripcio)
{
    InitializeComponent();
    this.descripcio = descripcio;
    inicialitza_descripcions();
}

private void inicialitza_descripcions()
{
    for (int i = 0; i < this.descripcio.Length; ++i)
    {
        System.Windows.Forms.Label Label = new System.Windows.Forms.Label();
        crea_label(ref Label2, 100, 50 * (i + 1), "D" + i, this.descripcio[i], 2 * i + 2);
        this.Controls.Add(Label);
    }
}

private void crea_label(ref System.Windows.Forms.Label Label, int x, int y, string nom, string text, int index)
{
    Label.AutoSize = true;
    Label.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    Label.Location = new System.Drawing.Point(x, y);
    Label.Name = nom;
    Label.TabIndex = index;
    Label.Text = text;
    Label.Visible = true;
    Label.MouseDown += new System.EventHandler(Label_MouseDown);
    Label.MouseUp += new System.EventHandler(Label_MouseUp);
    Label.MouseMove += new System.EventHandler(Label_MouseMove);
}

private void Label_MouseDown(object sender, MouseEventArgs e)
{  
    this.isDragging = true;
}  

private void Label_MouseUp(object sender, MouseEventArgs e)
{
    this.isDragging = false;  
}  

private void Label_MouseMove(object sender, MouseEventArgs e)
{
    Control ctrl = sender as Control;  
    if (this.isDragging)  
    {  
        Point p1 = ctrl.PointToScreen(e.Location);  
        Point p2 = ctrl.Parent.PointToClient(p1);  
        ctrl.Location = p2;  
    }  
}

This code generate an "no overload for "Label_MouseDown/Label_MouseUp/Label_MouseMove" matches delegate 'system.eventhandler'" error in this three lines:

    Label.MouseDown += new System.EventHandler(Label_MouseDown);
    Label.MouseUp += new System.EventHandler(Label_MouseUp);
    Label.MouseMove += new System.EventHandler(Label_MouseMove);

The first line "no overload for "Label_MouseDown" matches delegate 'system.eventhandler'". The second line "no overload for "Label_MouseUp" matches delegate 'system.eventhandler'". The third line "no overload for "Label_MouseMove" matches delegate 'system.eventhandler'".

Before to write this post, I have seen several posts with the similar error message. I tried to follow the explanations that I could read in that posts, but I cannot get the solution. Thanks for all.

Edit:

Finally, I only have to change this part of code:

Label.MouseDown += new System.EventHandler(Label_MouseDown);
Label.MouseUp += new System.EventHandler(Label_MouseUp);
Label.MouseMove += new System.EventHandler(Label_MouseMove);

for:

Label.MouseDown += new System.Windows.Forms.MouseEventHandler(Label_MouseDown);
Label.MouseUp += new System.Windows.Forms.MouseEventHandler(Label_MouseUp);
Label.MouseMove += new System.Windows.Forms.MouseEventHandler(Label_MouseMove);

That's all. Thanks HuorSwords.

Answer

HuorSwords picture HuorSwords · Apr 17, 2013

Your Label_MouseDown, Label_MouseUp and Label_MouseMove are not implementing System.EventHandler contract, that must be:

void Label_MouseDown(object sender, EventArgs e)
void Label_MouseMove(object sender, EventArgs e)
void Label_MouseUp(object sender, EventArgs e)

Additionally, you must cast the EventArgs parameter to MouseEventArgs in order to use it into your methods (actually you only are using it into your Label_MouseMove method).