0

I was wondering how to make a SplashScreen in C#. I looked around StackOverflow but found nothing useful, can you help me please? I already have 1 form filled with stuff, I just need some simple instructions on how to:

  • Make a new form appear before my main form
  • Make the SplashScreen disappear and the main form appear after a few seconds (maybe three)

I don't need an animated splashscreen.
Thanks in advance!

Just ask if you need any code samples from me :)

puretppc
  • 3,232
  • 8
  • 38
  • 65
Pyroglyph
  • 164
  • 5
  • 14
  • Welcome to this site. Please try to show some knowledge with the code. Also, try this question: http://stackoverflow.com/questions/1673568/splash-screen-display-method-best-practice-c-sharp Or this: http://stackoverflow.com/questions/2455703/splash-screen-example – puretppc Dec 10 '13 at 21:41
  • 1
    "I looked around StackOverflow but found nothing useful"? Unlikely. I found the duplicate (along with many others) by pasting your title into Google. – Zong Dec 10 '13 at 21:42

1 Answers1

3

Here is an example of a splashscreen - I have used in one of my projects - that uses multithreading:

namespace  WindowsForm1
{
    public enum SplashTypeOfMessage
    {   Success,    
        Warning,
        Error       
    }

    public partial class SplashForm : Form
    {
        static SplashForm _splashForm = null;
        static Thread _splashThread = null;
        public static object locker = new object(); 
        public  static bool WaitPlease = true;  

        private  SplashForm()
        {
            InitializeComponent();
            lblLoading.Text = "Please wait Loading";
        }

        public static  void ShowSplashScreen()
        {            
            if (_splashForm != null)
                return;
            _splashThread = new Thread(new ThreadStart(SplashForm.ShowSplash));
            _splashThread.IsBackground = true; 
            _splashThread.SetApartmentState(ApartmentState.STA) ; 
            _splashThread.Start();
        }

        public static void ShowSplash()
        {
            if (_splashForm==null)
            {                
                _splashForm = new SplashForm();
                _splashForm.blueLoaderBar1.StartAnimation();

            }
              _splashForm.TopMost = true;
              _splashForm.Show();
              lock (SplashForm.locker)
              {
                  WaitPlease = false;
              }

            Application.Run(_splashForm);

        }

        delegate void CloseSplashHandler(SplashTypeOfMessage typeOfMessage, string message,bool itWasRinvoked);

        public static void CloseSplash(SplashTypeOfMessage typeOfMessage,string message,bool itWasrinvoked)
        {                             
            CloseSplashHandler closeSpalshHandler = new CloseSplashHandler(CloseSplash);
            bool launched=false;
            while (!launched && !itWasrinvoked)
            {
                lock (SplashForm.locker)
                {
                    if (!SplashForm.WaitPlease)
                    {
                        launched = true;
                    }
                }
             }

            if (_splashForm!=null && _splashThread!=null )
            {
                if (_splashForm.InvokeRequired)
                {
                    _splashForm.Invoke(closeSpalshHandler,new object[] {typeOfMessage,message,true});
                }
                else
                {                    
                    switch (typeOfMessage)
                    {
                        case SplashTypeOfMessage.Warning:
                            break;
                        case SplashTypeOfMessage.Error:
                            MessageBox.Show("Error");                                                  
                            break;
                        default:
                            break;
                    }
                    _splashForm.Close();
                    _splashThread = null;

                }                                
            }
        }             
    }
}

Here is how you can call it:

SplashForm.ShowSplashScreen();

This is how you can close the splash screen:

SplashForm.CloseSplash(typeOfMessage ,string.Empty,false);
Brian
  • 5,069
  • 7
  • 37
  • 47
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
  • I have implemented your code and I think i need to add some using System.Something statements at the start, at the moment i have the default ones: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; Edit: I have removed some unneeded snippets but my IDE still doesn't recognise 'Thread'. – Pyroglyph Dec 10 '13 at 22:34
  • *cough* Get this: http://www.jetbrains.com/resharper/ – Meirion Hughes Dec 10 '13 at 22:40
  • @Pyroglyph you need using System.Threading; – BRAHIM Kamel Dec 10 '13 at 22:43
  • Okay I used common sense and tried using System.Threading and it seemed to work although Im not sure where to put SplashForm.ShowSplashScreen(); it wont show if i put it in SplashForm.cs and if i put it in my main form it insists that SplashForm doesnt exist in the current context when nearly everything in SplashForm is public! – Pyroglyph Dec 10 '13 at 22:49
  • in you main method Application.EnableVisualStyles(); bool error = false; Application.SetCompatibleTextRenderingDefault(false); SplashForm.ShowSplashScreen(); – BRAHIM Kamel Dec 10 '13 at 22:50
  • Im quite new to C# as you might have guessed but I put your code into SplashForm.cs and i thought my main method was in my main cs file (which isnt splashform.cs)? But i get a 'not in context' error. And the examples you gave me (Application.EnableVisualStyles(); bool error = false; Application.SetCompatibleTextRenderingDefault(false);) dont actually appear on my code so im a bit stuck here :( – Pyroglyph Dec 10 '13 at 22:57
  • Ah i found the bits you referenced but i still get a not in the current context error with this:` static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); SplashForm.ShowSplashScreen(); Application.Run(new offlineWiki()); }` – Pyroglyph Dec 10 '13 at 23:02
  • I dont have line numbers but i can give the whole Program.cs: using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace OfflineMinecraftWiki { static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); SplashForm.ShowSplashScreen(); Application.Run(new offlineWiki()); } } } – Pyroglyph Dec 10 '13 at 23:11