vb.net activate a window

Hello,
when I run this from visual studio it works perfectly fine.. when I compile the application and run the exe file it does not work.. why is that?

    Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As IntPtr
    Public Declare Function ShowWindow Lib "user32" (ByVal handle As IntPtr, ByVal nCmdShow As Integer) As Integer
    Public Declare Function BringWindowToTop Lib "user32" (ByVal HWnd As IntPtr) As Boolean
    Public Declare Function SetActiveWindow Lib "user32" (ByVal hWnd As Long) As Long
    Public Declare Function SetFocus Lib "user32.dll" (ByVal hwnd As Int32) As Int32



    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim hwnd As IntPtr

        hwnd = FindWindow(Nothing, Me.Tag)

        If hwnd = 0 Then
            MsgBox("app not found")
        Else
            SetForegroundWindow(hwnd)
            ShowWindow(hwnd, 1)
            BringWindowToTop(hwnd)
            SetActiveWindow(hwnd)
            SetFocus(hwnd)
        End If
    End Sub

Open in new window

LVL 1
XK8ERAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
It does not work as in you get the MessageBox?...or your window simply doesn't activate?

Some of your variable types are wrong.

Change:

    Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As IntPtr

To:

    Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As IntPtr) As Integer

And:

    Public Declare Function SetActiveWindow Lib "user32" (ByVal hWnd As Long) As Long

To:

    Public Declare Function SetActiveWindow Lib "user32" (ByVal hWnd As IntPtr) As Integer
0
XK8ERAuthor Commented:
okay I finally found what I needed..

SwitchToThisWindow(Me.Handle, True)

Public Declare Sub SwitchToThisWindow Lib "user32.dll" (hWnd As IntPtr, fAltTab As Boolean)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
XK8ERAuthor Commented:
i found the answer myself..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.