Getting Mouse Position over an Object

ducko

New member
Joined
Jun 28, 2010
Messages
1
Programming Experience
Beginner
Right now I have a program in which I'm trying to read the mouse cursor position relative to the window form, but I am currently having a problem. It only works if the mouse is directly over the form, and it will not work if I hold the mouse over an object, such as a label, a command button, or a picturebox. I tried resolving this by adding handle clauses for mousemove over those objects, however, while it works for the label, it doesn't work for the pictureboxes. This is because the pictureboxes are in an array and are defined at runtime. How would I go about resolving this?

code in question
VB.NET:
    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove, lblStat.MouseMove

        mcx = Math.Truncate(e.X / 16)
        mcy = Math.Truncate(e.Y / 16)

    End Sub

The array I am attempting to add to the handle clause is Grid(15,15) as Picturebox
 
As you create each picture box for the array you need to add the handler to it at that time.
VB.NET:
 Dim pb As New PictureBox
 AddHandler pb.MouseMove, AddressOf Form1_MouseMove
 
Back
Top