Click here to Skip to main content
14,575,793 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have written an application to aid releasing our internal systems (compressing, setting release information etc.). Everything works fine, except I get some odd behaviour.

When the MsgBox pops up saying "Rollout Successful", if you click the OK button, the application disappears from the screen (as in moves to the back), and a different application takes focus. Sometimes the application disappears from the Windows taskbar too (though can be Alt+Tabbed to, and visible if you minimise the other windows)

I've also noticed this happening inside our main application (It's an MDI parent calling DlgForm.ShowDialog(Me). I've even tried putting Me.Activate afterwards to try and recapture focus), but have no idea why it happens or even how to debug it. I never used to have this issues with MsgBox.

Does anybody know why this happens? or any ideas on how to fix it?

Private Sub ReleaseApp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReleaseApp.Click
  Dim res As Assembly

  If ReleaseType.Text = "" Then
    MsgBox("No reason for release selected")
    Exit Sub
  End If

    res = BuildRolloutAssembly()
    If res Is Nothing Then Throw New Exception("Could not create assembly")
    Dim relObj As Object = res.CreateInstance("ReleaseObject")
    Dim t As Type = res.GetType("ReleaseObject")
    If CBool(t.InvokeMember("Release", BindingFlags.InvokeMethod, Nothing, relObj, New Object() {NewVersion.Text, RelNotes.Text, resourceFolder, ReleaseType.Text}, Nothing, Nothing, Nothing)) Then
      MsgBox("Rollout Successful", MsgBoxStyle.Information)
      MsgBox("Rollout Failed!", MsgBoxStyle.Exclamation)
    End If
  Catch ex As Exception
    MsgBox("Rollout Failed!", MsgBoxStyle.Exclamation)
  End Try

End Sub

If I create a new project with the following:
Public Class Form1

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    MessageBox.Show("Hello 2")
  End Sub

  Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Dim x As New Form1
  End Sub

End Class

The application never loses the focus, regardless of which buttons you press. This would indicate that something else is causing this behaviour
Updated 3-Apr-11 23:20pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

First, don't use MsgBox. Use either the System.Windows.Forms.MessageBox, which gives you more options and a bit greater control, or roll your own MessageBox class and use that instead.
MarqW 4-Apr-11 3:03am
That doesn't actually answer my question. Especially when I said that it happens when I also use forms using ShowDialog
Dave Kreskowiak 4-Apr-11 7:46am
Actually, it does. The MsgBox and MessageBox functions both do this, but noone has ever come up with an explanation. It's SUPPOSED to happen when use use a certain option in the MessageBox function when called from a Service that's tagged "Allow interact with Desktop", but it also happens without that option from normal applications.

That's why the advice to roll your own messagebox class. All you need is a small form and a little work.
MarqW 4-Apr-11 8:13am
The MessageBox.Show() seems to work in the RollOut. However, as I said, for some reason when using rolling our own dialogs in the main application, it will still occasionally fall to the back of the screen. Maybe it's just a bug we've got to live with.
Dave Kreskowiak 4-Apr-11 8:17am
I misunderstood the dialog form in the MDI application. Try removing the the "Me" from DlgForm.ShowDialog(Me).
MarqW 4-Apr-11 8:32am
If I add the DoEvents before the Me.Activate(), it keeps the application active. Otherwise it dives off regardless of the Me in ShowDialog is there or not. It's not ideal, but it works.

Friend Function AttachDialogWindow(ByVal newWindow As Form, ByVal disposeAtInvisible As Boolean) As Form

If disposeAtInvisible Then
End If



Return newWindow
End Function
Dave Kreskowiak 4-Apr-11 19:04pm
DoEvents may work, but it also introduces other issues. DoEvents forces the app to process ALL pending messages waiting in the pump before execution continues in the method that called DoEvents. This includes all mouse and keyboard messages, giving the user the opportunity to click other buttons that would not be valid pending the outcome of the current method call, possibly invalidating data.
Rate this:
Please Sign up or sign in to vote.

Solution 2

try API call for message box
Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As _
    Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As _
    Long) As Long
MarqW 4-Apr-11 5:16am
I've not tried that, because it won't help with the dialog windows

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100