Home > OS >  Bring Windows Forms window back to front after opening an external application
Bring Windows Forms window back to front after opening an external application

Time:01-01

I am opening from my VB.net Windows Forms application a PDF:

Try
    Process.Start(fileName)
Catch e As System.ComponentModel.Win32Exception 'no PDF viewer installed, use browser
    Dim startinfo As New ProcessStartInfo
    startinfo.FileName = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
    startinfo.Arguments = String.Format(fileName)
    startinfo.UseShellExecute = False
    Process.Start(startinfo)
End Try

I want my application form to come back to front after the PDF is opened. I have tried all of those, but neither works:

Me.Activate()
Me.BringToFront()
Me.TopMost = True        
Me.TopMost = False

Just using Me.TopMost=True in fact works, but I do not want to enforce my application to be in front of all others windows. I just want to bring it to front once after PDF opening. As soon as I add the command Me.TopMost = False to reset it, it does not work any more.

CodePudding user response:

I don't know if you can use a specific API to do what you ask, but from my point of view you have already found the solution:

Me.TopMost = True        
Me.TopMost = False

You have to use these two lines right after the end of the PDF opening operations.

Here is an example using the following code:

Imports System.Threading

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.BackColor = Color.Green
        Label1.Text = "Start"
    End Sub

    Private Sub PdfOperation_Simulation(sender As Object, e As MouseEventArgs) Handles Label1.MouseDown
        Me.BackColor = Color.Red
        Label1.Text = "Wait..."
        Me.Refresh()
        Thread.Sleep(3000) 'Pdf opening... I manually bring to front VisualStudio
        Me.TopMost = True
        Me.TopMost = False
        Me.BackColor = Color.Green
        Label1.Text = "Finish"
    End Sub

End Class

Output:

Example gif


UPDATE

It just does not work for me. I have simplified my code to Dim startinfo As New ProcessStartInfo startinfo.FileName ="C:\README.TXT" startinfo.UseShellExecute = True Process.Start(startinfo) Me.TopMost = True Me.TopMost = False This opens notepad, but my application is not coming to front.

This is because the notepad window showing operation take some time. Depending on your needs, you can simply wait some seconds by using a Thread.Sleep just before Me.TopMost = False or do something like this:

Dim startinfo As New ProcessStartInfo
startinfo.FileName = "C:\README.TXT"
startinfo.UseShellExecute = True
Process.Start(startinfo)

Dim t As New Thread(
    Sub()
        Thread.Sleep(1000)' One second
        Me.Invoke(Sub() MoveOnTop())
    End Sub
)
t.Start()

Where MoveOnTop is

Private Sub MoveOnTop()
    Me.TopMost = True
    Me.TopMost = False
End Sub

Using the thread, your application will not be frozen by the Sleep operation.

Just another option. Have you considered setting notepad's WindowStyle to minimized? Here is an example:

Dim startinfo As New ProcessStartInfo("Notepad")
startinfo.Arguments = "C:\README.TXT"
startinfo.UseShellExecute = True
startinfo.WindowStyle = ProcessWindowStyle.Minimized
Process.Start(startinfo)
  • Related