1

This might be a very common question but I cannot find a proper answer.
Mark this duplicate with the original link if found any..

Say, I have a winform with a button.
And I want to open another application such as say a windows calculator.
so on Button Click, I added this.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 Dim MyApp As System.Diagnostics.Process = New System.Diagnostics.Process
            MyApp.StartInfo.FileName = "C:\Windows\system32\calc.exe"
            MyApp.Start()


            Dim p() As Process = Process.GetProcessesByName("Calculator")
            If p.Count > 0 Then
                 /*Something like bring that calc to front*/
            Else
                MyApp.Start()
            End If
        End If
End Sub

Now I want something like to bring calc to the front if the calc is behind the winform.

If there is no calc open so it'll open new else it'll bring previous one to front.

bonny
  • 688
  • 1
  • 14
  • 33

1 Answers1

3

Specifically for Calculator:

Public Declare Function BringWindowToTop Lib "user32" (ByVal HWnd As IntPtr) As Boolean

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim calc As Process = Process.GetProcesses.FirstOrDefault(Function(p) p.MainWindowTitle = "Calculator")
    If Not IsNothing(calc) Then
        BringWindowToTop(calc.MainWindowHandle)
    Else
        Process.Start("C:\Windows\system32\calc.exe")
    End If
End Sub
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40