没有重载的匹配委托'system.eventhandler'MouseDD MouseUp MouseMove

时间:2013-04-17 09:57:21

标签: c# event-handling mousemove mousedown mouseup

我必须根据名为“descripcio”的字符串[]创建表单中的标签。 “descripcio”的每个参数对应于新标签。 每个标签需要三个事件,MouseDown,MouseUp和MouseMove。所有标签都有相同的事件。代码是:

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;  
    }  
}

此代码生成“无重载”Label_MouseDown / Label_MouseUp / Label_MouseMove“匹配委托'system.eventhandler'”这三行错误:

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

第一行“没有重载”Label_MouseDown“匹配委托'system.eventhandler'”。 第二行“没有重载”Label_MouseUp“匹配委托'system.eventhandler'”。 第三行“没有重载”Label_MouseMove“匹配委托'system.eventhandler'”。

在撰写这篇文章之前,我已经看过几篇有类似错误消息的帖子。我试着按照我在帖子中读到的解释,但我无法得到解决方案。谢谢大家。

编辑:

最后,我只需要更改这部分代码:

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

有:

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);

这就是全部。谢谢HuorSwords。

1 个答案:

答案 0 :(得分:1)

您的Label_MouseDownLabel_MouseUpLabel_MouseMove未实施System.EventHandler contract,必须是:

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

此外,您必须将EventArgs参数转换为MouseEventArgs才能将其用于您的方法(实际上您只是将其用于Label_MouseMove方法)。