How to create a SplashScreen in C#

By FoxLearn 12/1/2024 5:11:41 AM   7.76K
To create a splash screen in a C# application using DevExpress, you can utilize the SplashScreenManager class provided by the DevExpress framework.

The splash screen provides a professional touch and informs users that the application is starting, especially when it is initializing large resources or performing time-consuming tasks. In this article, we will demonstrate how to add a splash screen to a Windows Forms application using DevExpress

How to create a SplashScreen in C#?

Open Viual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "SplashScreenExample" and then click OK

Right-click your project in the Solution Explorer, then select Add > New Item > DevExpress Splash Screen Form.

This will create a new form with a splash screen template. You can customize the appearance as needed, such as adding a logo, progress bar, or any custom messages.

frmSplashScreen

devexpress splash screen

You can change the properties of the splash screen form as you see fit, like adding a progress bar or timer.

frmMain

devexpress c#

We will be working with two forms in this example: frmMain and frmSplashScreen. frmSplashScreen will display the splash screen, and frmMain will be the main application form.

You need to add a SplashScreenManager to your windows form application

In your frmMain, we simulate loading data by using a loop with a delay. The splash screen will be shown during this time.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Threading;

namespace SplashScreenExample
{
    public partial class frmMain : DevExpress.XtraEditors.XtraForm
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            // Simulating data loading...
            for(int i = 0; i < 100; i++)
            {
                Thread.Sleep(100); // Simulate work by sleeping for 100ms each time
            }
        }
    }
}

The frmMain_Load event simulates a task (loading data) by iterating through a loop and pausing for 100 milliseconds on each iteration with Thread.Sleep(100).

The frmSplashScreen form will serve as the splash screen shown to users while the application is loading. We use the SplashScreen base class provided by DevExpress to handle the splash screen.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraSplashScreen;

namespace SplashScreenExample
{
    public partial class frmSplashScreen : SplashScreen
    {
        public frmSplashScreen()
        {
            InitializeComponent();
        }

        #region Overrides

        public override void ProcessCommand(Enum cmd, object arg)
        {
            base.ProcessCommand(cmd, arg);
        }

        #endregion

        public enum SplashScreenCommand
        {
        }
    }
}

The frmSplashScreen inherits from SplashScreen, which is a DevExpress class for handling splash screens.

The ProcessCommand method is overridden, allowing you to handle specific commands during the splash screen’s lifecycle.

In the Program.cs file, we will use the SplashScreenManager to show the splash screen while the main form is being loaded. The SplashScreenManager manages the splash screen's visibility and lifecycle.

// Show splash screen
SplashScreenManager.ShowForm(typeof(frmSplashScreen));

// Simulate loading of the main form (simulated with a delay)
System.Threading.Thread.Sleep(3000);  // Simulate a 3-second initialization time

// Close splash screen after initialization
SplashScreenManager.CloseForm();

// Start the main form
Application.Run(new frmMain());

The SplashScreenManager.ShowForm(typeof(frmSplashScreen)); line shows the splash screen before loading the main form.

The Thread.Sleep(3000); simulates a 3-second loading process. During this time, the splash screen is visible to the user.

After the initialization is complete, SplashScreenManager.CloseForm(); is called to hide the splash screen.

Finally, Application.Run(new frmMain()); launches the main form of the application.

You can customize the splash screen by adding various elements such as progress bars, logos, and additional text to inform users about the current status of the loading process.

VIDEO TUTORIAL