Hello,
I was wondering how to set focus to a program but what I tried did not work. Any Ideas
And basically what I have is a typing bot, and I need the timer when it sends the message to open the specific program it needs to send the message to instead of just what usually happens (Finds the start menu and searches.) But any way here is what I tried.

Private Spammer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer1.Tick
        'Dim hwnd As Long
        'hwnd = FindWindow(vbNullString, "Microsoft Flight Simulator X")
        'OpenIcon(hwnd)
        'SetForegroundWindow(hwnd)
        'Send(TextBox1.Text)

        Dim myAppId As Object
        myAppId = Shell("c:\windows\FSX\Flight Simulator X.exe", AppWinStyle.NormalFocus)
        AppActivate(myAppId)
        SendKeys.Send("**Robot**")
        SendKeys.Send(TextBox1.Text)
        SendKeys.Send("{ENTER}")

But that never Works for some reason, I Will get the error code and put it in this topic.

Any Help would be awesome. :)

Hope this helps This was part of my program :idea:

Imports System.Windows.Forms.SendKeys

Public Class Form1
    Private Declare Function OpenIcon Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
    Dim WithEvents timer1 As Timer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        timer1 = New Timer
        timer1.Interval = 20000
        timer1.Start()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        timer1.Stop()
        TextBox1.Enabled = True
    End Sub

    Private Sub timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer1.Tick
        'Dim hwnd As Long
        'hwnd = FindWindow(vbNullString, "Microsoft Flight Simulator X")
        'OpenIcon(hwnd)
        'SetForegroundWindow(hwnd)

        'Send(TextBox1.Text)

        Dim myAppId As Object
        myAppId = Shell("c:\windows\FSX\Flight Simulator X.exe", AppWinStyle.NormalFocus)
        AppActivate(myAppId)
        SendKeys.Send(TextBox1.Text)
        TextBox1.Enabled = False
    End Sub
End Class

Okay Thanks I Will try it.

No, That did not work, any other Ideas

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.