none
Get cursor position in child form? RRS feed

  • Question

  • Hello,

    I have an external program embedded in a panel in my form, and I need the X,Y coordinates of my mouse position to display in a textbox, but only reletive to that form (i.e. if I were to move my cursor to the top left of the child form, the coordinate would probably be: 0, 0 - not the coordinates of client or screen).

    What I've tried:

        Private Sub ContainerPanel_MouseMove(sender As Object, e As MouseEventArgs) Handles ContainerPanel.MouseMove
            MyXY.Text = (e.X & ", " & e.Y)
        End Sub

    This works only with the actual panel, but not when the external application is in the panel, as the panel body itself is not visible. Is there any work-around with this?

    Thanks in advance!

    Sunday, November 5, 2017 12:16 PM

Answers

  • The relative mouse position can be determined at any time:

       Dim p = ContainerPanel.PointToClient(Cursor.Position)

    If the MouseMove event does not happen in your case, the simple way is to apply the above technique periodically using a Timer component and its Tick event. It depends on details.

    • Proposed as answer by IronRazerz Sunday, November 5, 2017 3:12 PM
    • Marked as answer by Lucy Qu Sunday, November 5, 2017 3:26 PM
    Sunday, November 5, 2017 1:32 PM
  •  Do as Viorel mentioned,  use a Timer to update the textbox with the coordinates....

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Timer1.Interval = 50 'this would update approximately 20 times a second
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim mp As Point = Panel1.PointToClient(MousePosition)
            Me.Text = mp.ToString
        End Sub
    End Class
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by Lucy Qu Sunday, November 5, 2017 3:26 PM
    Sunday, November 5, 2017 3:11 PM

All replies

  • The relative mouse position can be determined at any time:

       Dim p = ContainerPanel.PointToClient(Cursor.Position)

    If the MouseMove event does not happen in your case, the simple way is to apply the above technique periodically using a Timer component and its Tick event. It depends on details.

    • Proposed as answer by IronRazerz Sunday, November 5, 2017 3:12 PM
    • Marked as answer by Lucy Qu Sunday, November 5, 2017 3:26 PM
    Sunday, November 5, 2017 1:32 PM
  •  Do as Viorel mentioned,  use a Timer to update the textbox with the coordinates....

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Timer1.Interval = 50 'this would update approximately 20 times a second
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim mp As Point = Panel1.PointToClient(MousePosition)
            Me.Text = mp.ToString
        End Sub
    End Class
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by Lucy Qu Sunday, November 5, 2017 3:26 PM
    Sunday, November 5, 2017 3:11 PM
  • Thank you Viorel, thank you Iron.
    Sunday, November 5, 2017 3:26 PM