Results 1 to 5 of 5

Thread: Label to show x / y cordinates of cursor.

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    8

    Label to show x / y cordinates of cursor.

    Hi Im working on a project containing a couple of forms. On the main form I want to labels in the status bar that show the X,Y location of the cursor when it moves over the form. I thought this code would do it but it doesnt work does anyone know why or have any advice?
    Code:
    Private Sub MainForm_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
            lblCursorX.Text = "X: " & e.X
            lblCursorY.Text = "Y: " & e.Y
    
        End Sub

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Label to show x / y cordinates of cursor.

    That code will not work on controls within form1, so if you have a panel that takes up the whole area of the form, that would make sense. You have to handle the mousemove events on all of the child controls of the form.

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

    Re: Label to show x / y cordinates of cursor.

    Note that, not only will you have to handle the MouseMove event of every control, you'll have to convert the coordinates from each controls system to the form's system. That's because the position reported by the MouseMove event is relative to the top, left corner of the control raising the event, not the form.
    vb.net Code:
    1. Dim mouseLocation As Point = Me.PointToClient(DirectCast(sender, Control).PointToScreen(e.Location))
    2.  
    3. lblCursorX.Text = "X: " & mouseLocation.X
    4. lblCursorY.Text = "Y: " & mouseLocation.Y
    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

  4. #4
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    462

    Re: Label to show x / y cordinates of cursor.

    "jmcilhinney"

    you missed 1 letter

    it's


    1. Dim mouseLocation As Point = Me.PointToClient(DirectCast(sender,Control).PointToScreen(me.Location))

    3. lblCursorX.Text = "X: " & mouseLocation.X
    4.
    lblCursorY.Text = "Y: " & mouseLocation.Y

    you forgot to add the "E"

    but this code only works when i grab the page and move it what's wrong with it ?
    Last edited by tweaker99; Mar 15th, 2009 at 06:06 AM.

  5. #5
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Label to show x / y cordinates of cursor.

    No, Jmc's code was correct. The value e is the parameter passed to the MouseMove event that provides you with the location of the mouse.

    What you need to do is write a routine that handles the mousemove for all controls on your form. I think the best way to add the event handler would probably be looping through all of your controls and using the AddHandler method.

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