-2

I have the following code for a project I'm working in class and I need to copy it about 9 times in total. the only thing that will change each time is the category number and the label name by 1. Do I HAVE to create a mousemove event inidividually for each or is there a way to do it in one sub procedure? I don't care if it is harder, or less efficient I would just like to see if it is possible and how. Thank you.

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

It has been said this may be a duplicate. If someone could tell me how to do that same thing with mousemove instead of click that would be perfect. Thank you.

user1486297
  • 43
  • 1
  • 6

1 Answers1

0

Multiple controls can share the same event. You just need to check the sender variable to see which one it was and process appropriately:

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
Joe Uhren
  • 1,342
  • 1
  • 13
  • 27