VB.Net and MB_TOPMOST for dialogs?

5.4k Views Asked by At

When left-clicking an application sitting minimized in the systray and displaying a messagebox, the dialog is out of focus the first time it's displayed, while it does have the focus the second time around.

Google seems to say that the solution is to configure the dialog with the MB_TOPMOST parameter, but the .Net MessageBox object doesn't seem to suppport this parameter.

So I tried the following with the non-.Net MsgBox, but it doesn't solve the problem:

Public Class Form1

    Private Sub LeftClick(sender As Object, e As EventArgs) Handles NotifyIcon1.Click
      'Work-around to prevent Windows from triggering Click then right-click
      Dim MyButton As System.Windows.Forms.MouseEventArgs = e
      If MyButton.Button = MouseButtons.Left Then

            'MessageBox apparently unable to handle MB_TOPMOST
        'MessageBox.Show(Str, "Output", ???? )

            'Doesn't work
        Const MB_TOPMOST As Integer = &H40000
        MsgBox("Hello there", MsgBoxStyle.OkOnly Or MB_TOPMOST, "Out of focus")

      End If
    End Sub
End Class

Has someone found a work-around?

Thank you.

1

There are 1 best solutions below

0
On

For that you can use the TopMost Property of MsgBox (Number 262144)

MsgBox("Hello there", 262144, Title:="Out of focus")

Edit: Another way to accomplish this is creating a temporary form

Using form = New Form() With {.TopMost = True}
    MessageBox.Show(form, "Hello there", "Out of focus")
End Using