Can I do a loop for this?

Duc Nguyen 101 Reputation points
2022-04-27T10:32:01.443+00:00

There will be about 10 more labels that I want to do the function just like these two labels. I am wondering if I can do a loop for all of those labels. And would it be better if I keep it like that or loop will be better?

I'm new, thanks for the help

'The first one.

   Private Sub lblBat1G_MouseMove(sender As Object, e As MouseEventArgs) Handles lblBat1G.MouseMove
        lblBat1G.ForeColor = Color.Red
    End Sub
    Private Sub lblBat1G_MouseLeave(sender As Object, e As EventArgs) Handles lblBat1G.MouseLeave
        lblBat1G.ForeColor = Color.Black
        Label2.Enabled = False
        Label2.Visible = False
    End Sub

    Private Sub lblBat1G_MouseHover(sender As Object, e As EventArgs) Handles lblBat1G.MouseHover
        Label2.Enabled = True
        Label2.Visible = True
    End Sub

'The second one.

    Private Sub lblBat2G_MouseMove(sender As Object, e As MouseEventArgs) Handles lblBat2G.MouseMove
        lblBat2G.ForeColor = Color.Red
    End Sub

    Private Sub lblBat2G_MouseLeave(sender As Object, e As EventArgs) Handles lblBat2G.MouseLeave
        lblBat2G.ForeColor = Color.Black
        Label3.Enabled = False
        Label3.Visible = False
    End Sub
    Private Sub lblBat2G_MouseHover(sender As Object, e As EventArgs) Handles lblBat2G.MouseHover
        Label3.Enabled = True
        Label3.Visible = True
    End Sub

'...There are about 8 more just like above.

Developer technologies | VB
0 comments No comments

Answer accepted by question author

LesHay 7,146 Reputation points
2022-04-27T10:44:01.73+00:00

Hi
Here is a sample showing how to deal with multiple controls event handling in one Sub. Basically, add all event names to the Handles clause then in code, find which control called the event handler and code accordingly.

 Private Sub lblBat1G_MouseMove(sender As Object, e As MouseEventArgs) Handles lblBat1G.MouseMove, lblBat2G.MouseMove
 ' NOTE: each Label added to the Handles clause

 ' cast the sender to a Label
 Dim lab As Label = DirectCast(sender, Label)

 ' decide which Label is being used and
 ' switch accordingly
 ' continue to add cases for other labels
 Select Case lab.Name
 Case "lblBat1G"
 ' code for Label here

 Case "lblBat2G"
 ' code for Label here

 Case Else
 ' not recognised
 End Select

 End Sub

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. LesHay 7,146 Reputation points
    2022-04-27T19:54:01.983+00:00

    Hi
    Yes, I believe so, depending on exactly how you have designed the Form. For a simple example, if you have all the Labels in (say) a GroupBox, then you can loop through all the labels quite easily. Even all the Labels anywhere (a bit more care needed there though). If you do that, then you can get a match and Hide/UnHide as needed - the idea there is that each Label would be visible if the label was up to and including the Label in the 'sender' parameter - or something along those lines. I am making some guesses as to your requirements. You would need to explain exactly what you are trying to achieve.

    Please start a new thread for this topic as it has moved out of scope of the original question.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.