Results 1 to 10 of 10

Thread: Getting Mouse Position over an Object

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    6

    Getting Mouse Position over an Object

    I am using VB.NET and I am currently having a problem accurately reading the mouse position. In my code, whenever the user moves the mouse, I want to have two variables store the mouse position's X and Y value, however when I move the mouse over an object such as a picturebox or a label, it will no longer give me the value until I move the pointer off the object onto the form. Are there any methods that will give me the absolute mouse coordinates regardless of whether the mouse is over an object or not?

  2. #2
    Lively Member
    Join Date
    Jul 2009
    Location
    Amsterdam, NY
    Posts
    85

    Re: Getting Mouse Position over an Object

    you could try this:

    vb.net Code:
    1. Dim mousePos As Point = MousePosition()
    2. Dim mousex As Integer = mousePos.X
    3. Dim mousey As Integer = mousePos.Y

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    6

    Re: Getting Mouse Position over an Object

    Nope, sorry still doesn't work. I've tried:

    e.x ; e.y
    Cursor.Position.X - (Me.Location.X + 28) ; Cursor.Position.Y - (Me.Location.Y + 28)
    and something else with PointToClient

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Getting Mouse Position over an Object

    That makes it sound like you are getting something, just not what you want. I haven't specifically looked at this, but I would expect that you are getting the position in screen coordinates. Does that sound right? What happened when you used PointToClient from the form?
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    6

    Re: Getting Mouse Position over an Object

    In every case I got the same result. The program correctly returns the mouse coordinates UNLESS the pointer is hovering over an object, such as a button, label, picturebox, etc.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Getting Mouse Position over an Object

    It sounds like you're handling the MouseMove event of the form but not of the controls on the form. If you want to be able to track the mouse pointer over any control then you must handle the appropriate event of every control. You can use a single method and just add every control and the form to the Handles clause, which you can do in the Properties window.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    6

    Re: Getting Mouse Position over an Object

    Thank you. I have almost solved my problem now. I know have the mouse moving correctly over the picture boxes, however I am still encountering one problem. Since I have initialized an array of pictureboxes at runtime, how would I go about adding those to the Handle clause? It does not simply let me add Grid().MouseMove, or anything like that.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Getting Mouse Position over an Object

    When you add items to the list, you will have to add the handler directly with:

    AddHandler <event you want to handle>,AddressOf <Sub that handles the event>
    My usual boring signature: Nothing

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Getting Mouse Position over an Object

    Just be sure that, for every AddHandler, there's a corresponding RemoveHandler when you're done with the object.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    6

    Re: Getting Mouse Position over an Object

    All right then, one more thing and then I'll be all good. Now it successfully detects mouse movement over all parts of the image, however, when it returns the mouse coordinates, it does it relative to whatever object the mouse is hovering over. Is there anyway for it to consistently return the mouse coordinates relative to the frame all the time?

    edit: Shell yeah, never mind I gots this one, I just replaced that e.X thing I had with the other thing involving Cursor.Position.X that I had earlier, awesum then, thanks SHOO MUCHHH guys DDD
    Last edited by ducko; Jun 28th, 2010 at 08:48 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width