I have a Windows Forms application VS2010 C# which displays a MessageBox with an OK button.
How can I make it so that, if the user walks away, the message box automatically closes after a timeout of, say, 5 seconds?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try the following approach:
Where the
AutoClosingMessageBoxclass implemented as following:Update:
If you want to get the return value of the underlying MessageBox when user selects something before the timeout you can use the following version of this code:
Yet another Update
I have checked the @Jack’s case with
YesNobuttons and discovered that the approach with sending theWM_CLOSEmessage does not work at all.I will provide a fix in the context of the separate AutoclosingMessageBox library. This library contains redesigned approach and, I believe, can be useful to someone.
It also available via NuGet package:
Release Notes (v1.0.0.2):
Show(IWin32Owner)API to support most popular scenarios (in thecontext of #1 );
AutoClosingMessageBox.Factory()API to provide full control on MessageBox showing;Release Notes (v1.0.0.3):