Sunday, August 7, 2011

vb.net bring form to the front

Question How to bring an already running application in front


Focus On Another Application

set the focus - VB.NET

Form.BringToFront
 
I was programing in Visual Studio 2010. I ran into this problem and saw no answers in any Google searches. I Saw a whole lot of very complicated and even crazy work arounds so I'm posting this to possibly help someone else who runs into a similar issue.
 
The problem was when I tried to get focus on the new form after it opened it would always stay in the background. This subroutine simply checks to see if a window is open with a flag I set and if it is I focus on it. When the program closes it sets the flag back to 0. I could not get the window to focus when I used
 
Dim frm as New vtfrmsearch
 
by removing the Dim statement everything worked perfect.
 

Private Sub SearchTempToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchTempToolStripMenuItem.Click

     If SearchOpen = 1 Then
        vtfrmsearch.Focus()


     End If


    If SearchOpen = 0 Then
        vtfrmsearch.Show()
        SearchOpen = 1
    End If

End 


If you want a form to always stay on top until it is closed use:

vtfrmsearch.ShowDialog()


Sub

No comments: