Question:
I tried to find out how I could do this, but I didn't find anything clear. I want to know if it is possible to close the first msgbox
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
Answer:
This MsgBox()
is probably a legacy from 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 .NET MessageBox()
class does not allow it to be closed programmatically. Depending on the need ask for a different solution. I'll put some in order from the most correct to the most tricky. It should be noted that this type of control should not be programmatically closed. It wasn't an oversight not to have a way to close it. It is 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 doesn't make sense to use a control that doesn't do what you want. Note that MessageBox()
not designed to have multiple instances. It was created to block the application, to wait for an action on itself.
If you want a control that works almost exactly like MessageBox()
, then create your own control that does this and add a way to programmatically close it. But just putting this functionality in shows that what you want is not a MessageBox()
, it's a normal form that does some specific things.
There is a project in the Code Project that creates a code-closeable version.
Other than that, you can simulate that something is sent to the environment to force it to close. you can send a key to the controller.
There is even the possibility to capture the behavior of Windows messages at a lower level than Windows Forms and send a WM_CLOSE
.