C # WinForm - loading screen

I would like to ask how to make a loading screen (just an image or something else) that appears during program loading and disappears when the program has finished loading.

in fancier versions, I saw the process bar (%). how can you do this and how do you calculate% to show on it?

I know that there is a Form_Load () event, but I do not see the Form_Loaded () or% event as a property / attribute anywhere.

+9
c # screen loading
Apr 05 '13 at 14:05
source share
2 answers

all you need to create one form as a splash screen and show it before you start showing the landing page and close this splash after loading the landing page.

using System.Threading; using System.Windows.Forms; namespace MyTools { public class SplashForm : Form { //Delegate for cross thread call to close private delegate void CloseDelegate(); //The type of form to be displayed as the splash screen. private static SplashForm splashForm; static public void ShowSplashScreen() { // Make sure it is only launched once. if (splashForm != null) return; Thread thread = new Thread(new ThreadStart(SplashForm.ShowForm)); thread.IsBackground = true; thread.SetApartmentState(ApartmentState.STA); thread.Start(); } static private void ShowForm() { splashForm = new SplashForm(); Application.Run(splashForm); } static public void CloseForm() { splashForm.Invoke(new CloseDelegate(SplashForm.CloseFormInternal)); } static private void CloseFormInternal() { splashForm.Close(); splashForm = null; } } } 

and the main function of the program looks like this:

 [STAThread] static void Main(string[] args) { SplashForm.ShowSplashScreen(); MainForm mainForm = new MainForm(); //this takes ages SplashForm.CloseForm(); Application.Run(mainForm); } 
+26
Apr 05
source share

If you intend to show SplashForm more than once in your application, be sure to set the splashForm variable to null, otherwise you will receive an error message.

 static private void CloseFormInternal() { splashForm.Close(); splashForm = null; } 
+1
Mar 12 '14 at 17:00
source share



All Articles