Click here to Skip to main content
15,904,934 members
Please Sign up or sign in to vote.
4.00/5 (1 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?

VB
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

  Try
    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)
    Else
      MsgBox("Rollout Failed!", MsgBoxStyle.Exclamation)
    End If
  Catch ex As Exception
    MsgBox("Rollout Failed!", MsgBoxStyle.Exclamation)
  End Try

End Sub


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

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    MsgBox("Hello")
  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
    x.ShowDialog()
  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
Posted
Updated 3-Apr-11 23:20pm
v3

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.
 
Share this answer
 
Comments
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
newWindow.ShowDialog()

If disposeAtInvisible Then
newWindow.Close()
newWindow.Dispose()
End If

Application.DoEvents()

Me.Activate()

Return newWindow
End Function
try API call for message box
VB
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
 
Share this answer
 
Comments
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900