VB.NET - Mouse Coordinates

后端 未结 3 788
春和景丽
春和景丽 2021-01-24 16:52

I have a vb.net application, and I want to know how to find the coordinates of the pointer (mouse) when it is clicked on the form. Not much else to say, so I\'ll leave it like t

3条回答
  •  天涯浪人
    2021-01-24 17:30

    You can also try this

    Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
         TextBox1.Text = e.Location.ToString()
    End Sub
    

    I would not advise

    Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
         TextBox1.Text = MousePosition().ToString()
    End Sub
    

    because it may change after you first click down on the button because you moved your mouse. So its always better if you are doing a calculation to assign the location to a variable or in e.location case it is already a separate variable that doesn't change. This is also why it is a better choice than MousePosition since mouseposition will constantly change while its in this click function rather then e.location will remain the same till it leaves the mouseclick event.

提交回复
热议问题