How to hide a modal dialog without returning from .ShowDialog?

I have an application in vb.net that starts with a subfunction, performs some actions and decides whether it shows itself or not. When he shows himself, he does it by calling dialog.ShowDialog().

When it returns dialog.ShowDialog(), the application does some cleanup and shuts down.

I would 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 me.Hide()in the form code, the form is effectively hidden, but the ShowDialog () function returns and the process closes.

I understand that this is the expected behavior. So my question is, how can I get this effect? This is the start of a dialog that can be hidden and blocked until the user really wants to exit the application.

+5
source share
3 answers

You cannot do this work, ShowDialog () will always return when the form is hidden. The trick is to use the regular form and the regular call to Application.Run (), but to prevent it from being viewed 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

Be careful that the Load event handler does not fire until the form becomes visible, so be sure to initialize in the Sub New constructor.

+4

, ShowDialog(). , .

, .

form1.WindowState = FormWindowState.Minimized;

.

form.Left = -16384;

#

+5

.

  • : bool done = false;
  • set done = true, ( FormClosed).
  • ( ?)

    bool stilInMyFrm = false;
    MyFrm myFrm = new myFrm();
    
    try
    {
        stilInMyFrm = true;
        myFrm.ShowDialog();
        while (!myFrm.done)
            Application.DoEvents();
    }
    finally
    {
        stilInMyFrm = false;
        cleanup();
    }
    
-1

All Articles