Question

My Code:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim top As Integer = 0
        For i = 0 To 10
            Dim inLine As Integer = 8
            Dim left As Integer = 0
            For x = 0 To inLine
                Dim s As New Panel
                s.BackColor = Color.Black
                s.Width = 10
                s.Height = 10
                s.Left = left
                s.Top = top
                left = left + 20
                AddHandler s.MouseHover, AddressOf Panel1_MouseHover
                Me.Controls.Add(s)
            Next
            top = top + 20
        Next

    End Sub

        Private Sub Panel1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs)
   /////---- Some code!! :/ 
        End Sub

My code adding panels into my from ,
i want that when Mouse Hover on panel the panel change the background color.

if someone not understand:
when my mouse hover my panel that i add to my form, how i change the panel back color?

Private Sub Panel1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs)
       /////---- Some code to change the panel color !! :/ 
End Sub
Was it helpful?

Solution

I think the part you're missing is the fact that the sender parameter to the event handler method will always be whichever control it is that is raising the event. Before using it though, I would cast it to the correct type so you get the full advantages of intellisense and compiler type checking.

Private Sub Panel1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim panel As Panel = CType(sender, Panel)
    panel.BackColor = Color.White
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top