C # winForm: Wait for the implementation of the form,

Source: Internet
Author: User

C # winForm: Wait for the implementation of the form,

A waiting form is required for a project recently. The SplashScreen control can be used under DevExpress, And the ProgressIndicator control can be used. However, if Dev is not used for development, we need to customize a waiting form.

 

First, put it up:

The implementation function is relatively simple, that is, when the program processes some time-consuming code, the Loading form is displayed to the user and executed in the background.

 

This program refers to a program named "". When I was in the group, I thanked him for his enthusiastic help.

Now I have posted my code, which uses the concept of delegation. If you don't understand it, you can use Baidu. Here are several links:

Http://blog.csdn.net/ggz631047367/article/details/44646233

Http://www.runoob.com/csharp/csharp-delegate.html

Http://blog.csdn.net/sjj2011/article/details/7835200

Http://blog.csdn.net/testcs_dn/article/details/37671513

The LoadingControl. cs code is as follows:

1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. linq; 7 using System. text; 8 using System. windows. forms; 9 using System. threading; 10 11 namespace ControlToolsLibrary 12 {13 public partial class LoadingControl: Form 14 {15 16 public delegate void mydelegate (); 17 public mydelegate eventMethod; 18 priva Te static LoadingControl pLoading = new LoadingControl (); 19 delegate void SetTextCallback (string title, string caption, string description); 20 delegate void CloseFormCallback (); 21 public LoadingControl () 22 {23 InitializeComponent (); 24 initLoadintForm (); 25 Thread t = new Thread (new ThreadStart (delegateEventMethod); 26 t. isBackground = true; 27 t. start (); 28} 29 30 private void LoadingControl _ FormClosing (object sender, FormClosingEventArgs e) 31 {32 if (! This. isDisposed) 33 {34 this. dispose (true); 35} 36} 37 38 private void initLoadintForm () {39 this. controlBox = false; // The close button is not displayed. 40 this. startPosition = FormStartPosition. centerParent; 41} 42 43 private void delegateEventMethod () 44 {45 eventMethod (); 46} 47 48 public static LoadingControl getLoading () 49 {50 if (pLoading. isDisposed) 51 {52 pLoading = new LoadingControl (); 53 return pLoa Ding; 54} 55 else 56 {57 return pLoading; 58} 59} 60 61 // This method demonstrates how to call controls on Windows Forms in thread-safe mode. 62 // <summary> 63 // set the title of the Loading form, tag caption and description 64 // </summary> 65 // <param name = "title"> the title of the window [when it is null, take the default value] </param> 66 // <param name = "caption"> label (for example, please wait) [when it is null, take the default value] </param> 67 // <param name = "description"> description (for example, loading resources ...) [if it is null, take the default value] </param> 68 public void SetCaptionAndDescription (string title, string caption, string description) 69 {70 if (this. invokeRequired & Lo AdingControl. lbl_caption.InvokeRequired & LoadingControl. lbl_description.InvokeRequired) 71 {72 SetTextCallback d = new SetTextCallback (SetCaptionAndDescription); 73 this. invoke (d, new object [] {title, caption, description}); 74} 75 else 76 {77 if (! Title. Equals ("") {78 this. Text = title; 79} 80 if (! Caption. Equals ("") 81 {82 LoadingControl. lbl_caption.Text = caption; 83} 84 if (! Description. equals ("") {85 LoadingControl. lbl_description.Text = description; 86} 87} 88} 89 90 public void CloseLoadingForm () 91 {92 if (this. invokeRequired) 93 {94 CloseFormCallback d = new CloseFormCallback (CloseLoadingForm); 95 this. invoke (d, new object [] {}); 96} 97 else 98 {99 if (! This. isDisposed) 100 {101 this. dispose (true); 102} 103} 104} 105 106 public void SetExecuteMethod (mydelegate method) 107 {108 this. eventMethod + = method; 109} 110 111 112} 113}
View Code

The method to call Form is as follows:

 

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.