views:

157

answers:

2

I have an application in vb.net that starts with a sub function do some things and decide if it shows itself or not. When it shows itself it do so by invoking dialog.ShowDialog().

When dialog.ShowDialog returns the application does some cleaning and end.

I'd like to find a way to temporarily hide the dialog (send it to the system tray) without returning from the ShowDialog() function. However, as soon as I do a me.hide() in the form's code, the form is effectively hidden, but the ShowDialog() function return and the process is closed.

I understand this is the expected behaviors. So my question is how can I get this effect? That is launch a dialog, that can be hidden, and block until the user really wants to quit the application.

Thanks for your help.

+1  A: 

You cannot make this work, ShowDialog() will always return when the form is hidden. The trick is to use a regular form and a normal call to Application.Run() but to prevent it from becoming visible immediately. Paste this code into your form class:

Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
    If Not IsHandleCreated Then
        CreateHandle()
        value = false
    End If
    MyBase.SetVisibleCore(value)
End Sub

Beware that your Load event handler won't run until the form actually becomes visible so be sure to do any initialization in the Sub New constructor.

Hans Passant
@nobugz: Thanks, Application.Run was exactly what I needed.
Mathieu Pagé
A: 

If you hide the dialog, you will return from ShowDialog(). Forget about trying to change that, you can't.

You might be able to minimize the dialog.

form1.WindowState = FormWindowState.Minimized;

Or you can position it off screen.

form.Left = -16384;

Or you can make it transparent http://stackoverflow.com/questions/1360758/modifying-opacity-of-any-window-from-c

John Knoeller