How do I close/close a msgbox via code?

Asked

Viewed 1,527 times

5

I tried to find out how I could do this, but I did not find anything clear. I want to know if it is possible to close the first msgbox, shortly after 2 seconds.

Follows the Code:

MsgBox("Iniciando Conexão Com a Impressora Fiscal", MsgBoxStyle.Information, "Conexão")
Retorno = ECF_AbrePortaSerial()
If Retorno = "1" Then
    MsgBox("Conexão Estabelecida!", MsgBoxStyle.Information, "Ok")
Else
    MsgBox("Erro de Comunicação Com a Impressora Fiscal", MsgBoxStyle.Critical, "Erro")
    End
End If

2 answers

7


This MsgBox() It’s probably a legacy of VB6. You shouldn’t be using it in .NET. It won’t cause any big problems but it doesn’t make sense to use it in new code.

The class MessageBox() do. NET does not allow it to be closed programmatically. Depending on the need, it asks for a different solution. I’ll put some in the order of the most correct to the most gambiarra. It is good to point out that this type of control should not be closed programmatically. It was not forgetfulness not to have a way to close. It’s a mistake to try to close it.

The first solution you should use if you want a control that closes programmatically is to use another control. It makes no sense to use a control that does not do what you want. Note that the MessageBox() was not made to have multiple instances. It was created to block the application, to expect an action on itself.

If you want a control that works almost exactly like the MessageBox(), then create a control of your own that does this and add a way to close it programmatically. But the simple fact of putting this functionality shows that what you want is not a MessageBox(), is a standard form that does some specific things.

There’s a project in the Code Project which creates a version capable of closing by code.

Out of this you can simulate that something is sent to the environment to force the lock. you can send a key for control.

There is also the possibility to capture the behavior of Windows messages at a lower level than Windows Forms and send a WM_CLOSE.

  • Got it, thanks for the answer! Will help.

7

You can use the function MessageBoxTimeout, it has not been documented by Microsoft.

See a example in C#:

using System.Runtime.InteropServices;
//..
class MessageBoxTimer{
    [DllImport("user32.dll", SetLastError = true)]
    static extern int MessageBoxTimeout(IntPtr hwnd, String text, String title, uint type, Int16 wLanguageId, Int32 milliseconds);

    public enum MessageBoxReturnStatus{
        OK = 1, Cancel = 2, Abort = 3, Retry = 4, Ignore = 5, Yes = 6, 
        No = 7, TryAgain = 10, Continue = 11
    }

    public enum MessageBoxType{
        OK = 0, OK_Cancel = 1, Abort_Retry_Ignore = 2,
        Yes_No_Cancel = 3, Yes_No = 4,
        Retry_Cancel = 5, Cancel_TryAgain_Continue = 6
    }

    public MessageBoxReturnStatus Show(string title, string text, MessageBoxType type, int milliseconds){
        int returnValue = MessageBoxTimeout(IntPtr.Zero, text, title, Convert.ToUInt32(type), 1, milliseconds);
        return (MessageBoxReturnStatus)returnValue;
    }
}

Example of use:

private void Form1_Load(object sender, EventArgs e){
    MessageBoxTimer msg = new MessageBoxTimer();
    msg.Show("MessageBox Timeout", "Essa mensagem vai fechar em 5 segundos", 0, 5000);       
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.