So i'm new to VB and am making a program that is a count down timer and when it reaches '0' it clicks the left mouse button where ever the mouse is on the screen (in the form or not.
The code so far:

Public Class Form1
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        tmrWatch.Start()
    End Sub

    Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
        tmrWatch.Stop()
    End Sub
    Private Sub tmrWatch_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrWatch.Tick
        txtTenths.Text -= 1
        If txtTenths.Text = -1 Then
            txtTenths.Text = 9
            txtSeconds.Text -= 1
            If txtSeconds.Text = -1 Then
                txtSeconds.Text = 59
                txtMinutes.Text -= 1
                If txtMinutes.Text = -1 Then
                    txtMinutes.Text = 59
                    txtHours.Text -= 1
                    If txtHours.Text = -1 Then
                        txtHours.Text = 23
                        txtDays.Text -= 1
                        If txtDays.Text = -1 Then
                            ...............
                            tmrWatch.Stop()
                            txtTenths.Text = "0"
                            txtSeconds.Text = "0"
                            txtMinutes.Text = "0"
                            txtHours.Text = "0"
                            txtDays.Text = "0"
                        End If
                    End If
                End If
            End If
        End If
    End Sub

Where the dots are is where i want the code for 'left click' to go. but if it cant go there please show me where it can.

Recommended Answers

All 4 Replies

Hi,

You need a mousebutton click for some raison?

If you only want the position of your cursor then you don't need to perform a mouseclick. I've added a testbox to show you the coordinates of the cursor.

Public Class Form1

Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Integer
' This stores the cordinates of the mouse cursors location
    Dim mousepos As Point

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        tmrWatch.Start()
    End Sub

    Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
        tmrWatch.Stop()
    End Sub
    Private Sub tmrWatch_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrWatch.Tick
        txtTenths.Text -= 1
        If txtTenths.Text = -1 Then
            txtTenths.Text = 9
            txtSeconds.Text -= 1
            If txtSeconds.Text = -1 Then
                txtSeconds.Text = 59
                txtMinutes.Text -= 1
                If txtMinutes.Text = -1 Then
                    txtMinutes.Text = 59
                    txtHours.Text -= 1
                    If txtHours.Text = -1 Then
                        txtHours.Text = 23
                        txtDays.Text -= 1
                        If txtDays.Text = -1 Then
                             Dim R As Long = GetCursorPos(mousepos) ' You'll get your location in mousepos
  TextBox1.Text = ("Mouse Cursor Location : " & " X: " & mousepos.X & " Y: " & mousepos.Y)

                            tmrWatch.Stop()
                            txtTenths.Text = "0"
                            txtSeconds.Text = "0"
                            txtMinutes.Text = "0"
                            txtHours.Text = "0"
                            txtDays.Text = "0"
                        End If
                    End If
                End If
            End If
        End If
    End Sub

The reason i want this program is so i can start a download off steam(online game engine) at 3AM (the start of my off peak internet.)
And i definitely need a mouse click.

Hi,

Here's an example how to perform a Mouseclick:

Imports System.Runtime.InteropServices

Public Class Form1
    Inherits Form
    <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
        Public Shared Sub mouse_event(ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    End Sub

    Private Const MOUSEEVENTF_LEFTDOWN As Integer = &H2

        Public Sub DoMouseClick()
        'Call the imported function with the cursor's current position
        Dim X As Integer = Cursor.Position.X
        Dim Y As Integer = Cursor.Position.Y
        mouse_event(MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, X, Y, 0, 0)
        TextBox1.Text = (" Cursor position: X:" & X & " " & "Y:" & "" & Y)

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Start()
        Label1.Text = "0"
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        
        Label1.Text = Val(Label1.Text + 1)

        If Label1.Text = 10 Then
            DoMouseClick()
            Timer1.Stop()
        End If
    End Sub
End Class

Hi,

I forgot a variable and then you'll get an error. Add this one and it should work:

Private Const MOUSEEVENTF_LEFTUP As Integer = &H4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.