How do I close / close a msgbox via code?

4

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 , just after 2 seconds .

Follow 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
    
asked by anonymous 16.02.2015 / 12:54

2 answers

6

This MsgBox is probably a legacy of VB6. You should not be using it on. Net. It will not cause any major issues but it does not make sense to use it in new code.

The class MessageBox

16.02.2015 / 13:21
6

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

See a C # example :

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 usage:

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);       
} 
    
16.02.2015 / 13:35