You can set the MessageBox implementation that automatically disappears at a specified time to specify the messagebox
This article mainly describes how to implement the MessageBox prompt box ShowMessageBoxTimeout that can be set to automatically disappear at a specified time;
When developing client applications, the MessageBox prompt box in WinForm is often used. However, some user requirements may still not be met, and the customer's requirements may be odd. For example, when the customer needs to make some prompts and does not click OK or cancel, it will automatically disappear after a period of time, for this reason, we can use the following class for implementation, using Thread. sleep To turn off the current prompt box. The Code is as follows:
ShowMessageBoxTimeout implementation
Using System; using System. collections. generic; using System. linq; using System. text; using System. runtime. interopServices; using System. windows. forms; using System. threading; namespace Tools. app {public class ShowMsg {[DllImport ("user32.dll", SetLastError = true)] static extern IntPtr FindWindow (string lpClassName, string lpWindowName); [DllImport ("user32.dll")] static extern bool EndDialog (IntPtr hDlg, Out IntPtr nResult); // three parameters: 1. text prompt-text, 2. Prompt box title-caption, 3. Button type-MessageBoxButtons, 4. Set the automatic disappearance time-timeout public static void ShowMessageBoxTimeout (string text, string caption, MessageBoxButtons buttons, int timeout) {ThreadPool. queueUserWorkItem (new WaitCallback (CloseMessageBox), new CloseState (caption, timeout); MessageBox. show (text, caption, buttons);} private static void CloseMessageBox (object s Tate) {CloseState closeState = state as CloseState; Thread. Sleep (closeState. Timeout); IntPtr dlg = FindWindow (null, closeState. Caption); if (dlg! = IntPtr. Zero) {IntPtr result; EndDialog (dlg, out result );}}}}
ShowMessageBoxTimeout call
// Three parameters: 1. Text prompt; 2. Prompt box title; 3. Button type; 4. Set the automatic disappearance time to ShowMsg. showMessageBoxTimeout ("welcome to the data export service program. This program is minimized to the computer tray by default and will be officially started in 1 minute. "," Program startup tips-the window will be automatically closed if no operation is performed within one minute ", MessageBoxButtons. OK, 1000*60*1 );
I hope the above information will be helpful to beginners. Thank you!
More follow Fu Yi Fang technology blog: http://blog.csdn.net/fuyifang
Or scan the QR code on your mobile phone to view more blog posts: