1

I have an application that runs in the background and on command shows windows applications to the user.

My problem is that I can't get the windows to be on the front and on top of ALL the other windows (Browser(Usually) , other applications etc)

I used TopMost=True, BringToFront(), Form.Activate(), Form.ShowDialog() etc and it still not working for me.

I managed to get it in front of all other app but only at the first window. EX: I run the app in the background, and the first window is shown on top of all the others.(The windows basically shown on top only after reset) The user finished with this window and closes it. After a while a second window is supposed to be shown on top of all but its not at the top of all.

What else can I try? Do you have an idea of what can "block" my application so its not on top?

      Public Function ChooseDir() As String        
      Dim sRes As String = ""

        Using folders As frmFolderBrowser = New frmFolderBrowser()
            folders.ShowDialog()
            sRes = folders.StrPathValue
        End Using
      ChooseDir = sRes

      End Function



    Private Sub frmFolderBrowser_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load        
    If DialogResult.OK = FolderBrowserDialog.ShowDialog() Then
        strPath = FolderBrowserDialog.SelectedPath
    End If
    Me.Close()

End Sub
Guid2015
  • 57
  • 11
  • can you provide your code? It will be easy to understand. – Yog Dec 21 '15 at 08:27
  • I added the code @Yog – Guid2015 Dec 21 '15 at 08:40
  • The code you posted does not contain any code to show a form apart from the folder browser. Is that the window you want to bring to the front? – Matt Wilko Dec 21 '15 at 08:48
  • Yes, I want to show only the folders browser @Matt Wiko – Guid2015 Dec 21 '15 at 08:49
  • Try to set the owner when you call the ShowDialog IE: _If DialogResult.OK = folderBrowserDialog.ShowDialog(this) Then_ – Steve Dec 21 '15 at 09:00
  • It didn't help :-( @Steve – Guid2015 Dec 21 '15 at 09:15
  • Then I suppose there is something in the previous forms that leads to this code because, by itself the ShowDialog should make your Windows appear on top of your program. Try to not hide previous windows – Steve Dec 21 '15 at 09:19
  • 2
    Stealing the foreground is widely considered to be hostile to the user, by programmers and users alike. If you have something interesting to say to the user then use NotifyIcon.ShowBalloonTip(). If you merely hope that the user isn't busy using his machine then Form.BringToFront() can work. But VB.NET lets you be hostile, you can use AppActivate(). – Hans Passant Dec 21 '15 at 09:46
  • @Guid2015 an alternative that does work is trying the `SetWindowPos` function. You have to use the `user32.dll` for this to work, I have used this before and it work's fine. Cody Gray explains this well in an answer here: **http://stackoverflow.com/questions/8543765/bring-vb-net-window-on-top-of-all-windows** on SO – Trevor Dec 21 '15 at 12:46

1 Answers1

0

This should work for you:

MakeTopMostWindow(Me.Handle.ToInt64, True)
Application.DoEvents()
MakeTopMostWindow(Me.Handle.ToInt64, False)

where this is defined elsewhere

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer

Friend Sub MakeTopMostWindow(ByVal hwnd As Int64, ByVal MakeTopMostFlag As Boolean)

    Dim HWND_TOPMOST As Integer
    If MakeTopMostFlag Then
        HWND_TOPMOST = -1
    Else
        HWND_TOPMOST = -2
    End If

    Dim SWP_NOMOVE As Integer = &H2
    Dim SWP_NOSIZE As Integer = &H1
    Dim TOPMOST_FLAGS As Integer = SWP_NOMOVE Or SWP_NOSIZE
    Try
        SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS)
    Catch ex As Exception 
    End Try

End Sub
Rob
  • 3,488
  • 3
  • 32
  • 27