You can set the MessageBox implementation that automatically disappears at a specified time to specify the messagebox

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.