Hi there, i am a novice coder and i'm trying to comprehend the addhandler function.
on my current form, i have two pictureboxes, that i am trying to use in an addhandler with a loop, so when they are highlighted, the sender will change image.

Public Class Form1
    Dim i As Integer
    Dim picturebox As PictureBox() = {PictureBox1, PictureBox2}
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        For Me.i = 0 To 1
            AddHandler picturebox(i).MouseEnter, AddressOf highlight
        Next
    End Sub
    Private Sub Rollover(sender As Object, highlight As System.EventArgs)
        DirectCast(sender, PictureBox).ImageLocation = ("C:\Users\Steve _\Desktop\Untitle.png")
    End Sub
End Class

I understand i am doing something wrong here, can someone assist and explain what i am doing wrong and how this could be corrected? Thanks

This doesn't cause the error, but you should not use the form Me.i. Just use the variable name without the Me qualifier. As for the error, try the loop

For Each pbx As PictureBox In Me.Controls.OfType(Of PictureBox)()
    AddHandler pbx.MouseEnter, AddressOf highlight
Next

If that doesn't fix the problem then perhaps you could be more specific as to what the symptoms are.

Cheers dude. While that does help, the true problem i have lies in the Addhandler. I need it so when one of the pictureboxes are rolled over, the image will change, but it must be done with the addhandler function, i just dont really know how to work it.i dont know how to make it so the rollover sub will use the objects i have put in the addhandler as its handles.

I don't see the problem.

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    For Each pbx As PictureBox In Me.Controls.OfType(Of PictureBox)()
        AddHandler pbx.MouseEnter, AddressOf pbx_MouseEnter
    Next

End Sub

Private Sub pbx_MouseEnter(sender As System.Object, e As System.EventArgs)
    DirectCast(sender, PictureBox).ImageLocation = ("d:\temp\11213.jpg")
End Sub

works just fine for me, but you might want to do

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    For Each pbx As PictureBox In Me.Controls.OfType(Of PictureBox)()
        AddHandler pbx.MouseEnter, AddressOf pbx_MouseEnter
        AddHandler pbx.MouseLeave, AddressOf pbx_MouseLeave
    Next

End Sub

Private Sub pbx_MouseEnter(sender As System.Object, e As System.EventArgs)
    DirectCast(sender, PictureBox).ImageLocation = ("d:\temp\11213.jpg")
End Sub

Private Sub pbx_MouseLeave(sender As System.Object, e As System.EventArgs)
    DirectCast(sender, PictureBox).ImageLocation = Nothing
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.