mouseposition.x or mouseposition.y not really the position

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi, im trying to egt a picturebox to follow the mousecursor around in a
certain area, but the picturebox isnt where the mouse is...how can i get the
picturebox to be where the mouse cursor is? thanks
 
Hi,

Here is some sample code on how to move a label and button. I
hope it helps. The label I add to the form with code and button I dropped
on the form in the designer.

Dim bMoving As Boolean = False
Dim WithEvents lbl As Label
Dim bMovelbl As Boolean = False


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lbl = New Label
With lbl
.Location = New Point(10, 10)
.Text = "Move Me"
.BackColor = Color.Transparent
End With

Me.Controls.Add(lbl)

End Sub

Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
bMoving = True
End Sub


Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove
Static oldX, oldY As Integer

If bMoving Then
Button1.Location = New Point(Button1.Left + e.X - oldX,
Button1.Top + e.Y - oldY)
Else
oldX = e.X
oldY = e.Y
End If
End Sub

Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
bMoving = False
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
MessageBox.Show(Button1.Location.ToString)
End Sub

Private Sub lbl_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lbl.MouseDown
bMovelbl = True
End Sub

Private Sub lbl_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lbl.MouseUp
bMovelbl = False
End Sub

Private Sub lbl_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lbl.MouseMove
Static oldX, oldY As Integer

If bMovelbl Then
lbl.Location = New Point(lbl.Left + e.X - oldX, lbl.Top + e.Y -
oldY)
Else
oldX = e.X
oldY = e.Y
End If

End Sub


Ken
 
iwdu15 said:
hi, im trying to egt a picturebox to follow the mousecursor around in a
certain area, but the picturebox isnt where the mouse is...how can i get
the
picturebox to be where the mouse cursor is? thanks

How do you determine the position of the mouse? Are you using 'e.X'/'e.Y'
in a control's 'MouseMove' event handler? When doing so, you'll have to
place the control you want to move in this container, or you'll have to
convert the coordinates using 'Control.PointTo*'.
 
i was just using this for example

me.button1.top = me.mouseposition.y
me.button1.left = me.mouseposition.x

but that didnt work, i found a different way of moving it but il try
implimenting the mouse move thing i wanted to ealier, now that you guys told
me how to achieve this....thank you
 
I have found that if you use PointToClient you get a more accurate position for the mouse, like so:

LocalMousePosition = PictureBox1.PointToClient(Cursor.Position)

TextBox2.Text = ("X=" & LocalMousePosition.X & "," & "Y= " & LocalMousePosition.Y)


james
 
james said:
I have found that if you use PointToClient you get a more accurate position for the mouse, like so:

LocalMousePosition = PictureBox1.PointToClient(Cursor.Position)

TextBox2.Text = ("X=" & LocalMousePosition.X & "," & "Y= " & LocalMousePosition.Y)


james

Sorry, after re-reading your original post, I see that you are trying to get the Picturebox to follow the mousepointer, not get
accurate mouse coordinates within the picturebox. Instead of PointToClient, I would suggest you use PointToScreen to get
accurate info for the mouse pointer on your form.
james
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top