Results 1 to 2 of 2

Thread: Make a button follow the mouse.

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    10

    Make a button follow the mouse.

    I am trying to make a button follow the mouse but with the code below the actual button is maybe about 2 inches off from the mouse's pointer. The
    button is about 2 inches to the right and below of the cursor but it does follow around as intended, but for some reason when the form is maximised it is only about 1 inch to the right and 1 inch below the cursor.
    VB Code:
    1. Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
    2.         Dim MouseX As Integer = Me.MousePosition.X
    3.         Dim MouseY As Integer = Me.MousePosition.Y
    4.         Me.btnRun.Location = New Point(MouseX, MouseY)
    5.     End Sub
    Last edited by nclax99; Jan 13th, 2006 at 12:21 PM.

  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Make a button follow the mouse.

    I believe Me.MousePosition gives you the position of the mouse relative to the screen, while the button location is relative to it's container (i.e. the form, or a panel if the button is inside the panel). Use e to get the mouse corrdinates. That gives you the corrdinates relative to the container.

    VB Code:
    1. btnRun.Location = new point(e.x, e.y)

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