对于我在课堂上工作的项目,我有以下代码,我需要将其复制大约9次。每次更改的唯一事情是类别编号和标签名称1.我是否必须为每个事件单独创建一个mousemove事件,或者是否有办法在一个子程序中执行此操作?我不在乎它是否更难,或者更低效我想知道它是否可能以及如何。谢谢。
Private Sub lbl1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lbl1.MouseMove
category = 1
scoreRoll()
lbl1.Text = score
End Sub
据说这可能是重复的。如果有人能告诉我如何用mousemove做同样的事情,而不是点击那将是完美的。谢谢。
答案 0 :(得分:0)
多个控件可以共享同一个事件。您只需检查sender
变量以查看它是哪一个并进行适当处理:
Private Sub lbl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lbl1.MouseMove, lbl2.MouseMove, lbl3.MouseMove, lbl4.MouseMove, lbl5.MouseMove, lbl6.MouseMove, lbl7.MouseMove, lbl8.MouseMove, lbl9.MouseMove
Dim lbl As Label = CType(sender, Label)
category = CInt(lbl.Name.Replace("lbl", ""))
scoreRoll()
lbl.Text = score
End Sub