system.eventhandler'鼠标向下,鼠标向上,鼠标移动

本文关键字:鼠标 移动 system eventhandler | 更新日期: 2023-09-27 18:09:41

我必须根据名为"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"匹配委托系统生成了一个"no overload"。这三行Eventhandler ' '"错误:

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

第一行"no overload for "Label_MouseDown"匹配委托'system.eventhandler'"。第二行"no overload for Label_MouseUp"匹配委托"system.eventhandler"。第三行"no overload for "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。

system.eventhandler'鼠标向下,鼠标向上,鼠标移动

你的Label_MouseDown, Label_MouseUpLabel_MouseMove没有执行System.EventHandler合同,那必须是:

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方法中使用它)。