set mouse position and click

  • 14 years ago


    I want to set the mouse position programmatically and do a click.

    I used the code bellow, but don't work.

    Private Declare Function SetCursorPos Lib "user32.dll" (byval x as Long, byval y as Long) as Long


    I got an error: "A call to PInvoke function 'SetCursorPos' has unbalanced the stack" 

    How can I set the mouse position properly and do a click.



  • 14 years ago

    Given two buttons on a form, this code will move the mouse into the top right of the second button:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

      Dim pt As Point

      pt.X = Button2.Right - 10

      pt.Y = Button2.Top + 10

      pt = PointToScreen(pt)

      Cursor.Position = pt

    End Sub

    Will endeavour to look at simulating a generic mouse click - I've only ever needed to simulate mouse clicks on known objects, via control.PerformClick.  I'll post back here if I find anything.



Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter” - Eric Raymond