none
"Visual basic" 2010 "how to" "give focus to an application" RRS feed

Answers

  • I am looking for VB 2010 code example on how to set focus on a running application from a VB program.  The code at http://support.microsoft.com/kb/186431 doesn't work.  Thanks!

    Hi Ronny,

    Welcome to the MSDN Forum.

    The code is VB6 code rather than VB.net code, and for VB6 issue, please refer to this thread to find the VB6 forum: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/6a0719fe-14af-47f7-9f51-a8ea2b9c8d6b 

    If you are looking for VB.net code, please take a look at this(I translated it from the code in MusicDemon posted link):

    <DllImport("User32.dll")> _
    Private Shared Function SetForegroundWindow(hwnd As IntPtr) As Integer
    End Function
    
    <DllImport("User32.DLL")> _
    Private Shared Function ShowWindow(hWnd As IntPtr, nCmdShow As Integer) As Boolean
    End Function
    
    Private Const SW_SHOW As Integer = 5
    Private Const SW_MINIMIZE As Integer = 6
    Private Const SW_RESTORE As Integer = 9
    
    Private Sub ActivateApplication(briefAppName As String)
    	Dim procList As Process() = Process.GetProcessesByName(briefAppName)
    
    	If procList.Length > 0 Then
    		ShowWindow(procList(0).MainWindowHandle, SW_RESTORE)
    		SetForegroundWindow(procList(0).MainWindowHandle)
    	End If
    End Sub

    Thanks MusicDemon.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 29, 2012 2:00 PM
    Moderator

All replies

  • Not sure if Google was blocked in your country but, it gave me this.

    Regards, MusicDemon

    Thursday, June 28, 2012 11:08 AM
  • I am looking for VB 2010 code example on how to set focus on a running application from a VB program.  The code at http://support.microsoft.com/kb/186431 doesn't work.  Thanks!

    Hi Ronny,

    Welcome to the MSDN Forum.

    The code is VB6 code rather than VB.net code, and for VB6 issue, please refer to this thread to find the VB6 forum: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/6a0719fe-14af-47f7-9f51-a8ea2b9c8d6b 

    If you are looking for VB.net code, please take a look at this(I translated it from the code in MusicDemon posted link):

    <DllImport("User32.dll")> _
    Private Shared Function SetForegroundWindow(hwnd As IntPtr) As Integer
    End Function
    
    <DllImport("User32.DLL")> _
    Private Shared Function ShowWindow(hWnd As IntPtr, nCmdShow As Integer) As Boolean
    End Function
    
    Private Const SW_SHOW As Integer = 5
    Private Const SW_MINIMIZE As Integer = 6
    Private Const SW_RESTORE As Integer = 9
    
    Private Sub ActivateApplication(briefAppName As String)
    	Dim procList As Process() = Process.GetProcessesByName(briefAppName)
    
    	If procList.Length > 0 Then
    		ShowWindow(procList(0).MainWindowHandle, SW_RESTORE)
    		SetForegroundWindow(procList(0).MainWindowHandle)
    	End If
    End Sub

    Thanks MusicDemon.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 29, 2012 2:00 PM
    Moderator