我必须根据名为“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。