|
-
Mar 7th, 2009, 06:36 PM
#1
Thread Starter
New Member
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
-
Mar 7th, 2009, 06:43 PM
#2
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.
-
Mar 7th, 2009, 10:41 PM
#3
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:
Dim mouseLocation As Point = Me.PointToClient(DirectCast(sender, Control).PointToScreen(e.Location)) lblCursorX.Text = "X: " & mouseLocation.X lblCursorY.Text = "Y: " & mouseLocation.Y
-
Mar 15th, 2009, 05:56 AM
#4
Hyperactive Member
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.
-
Mar 15th, 2009, 08:17 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|