New to Telerik UI for WinForms? Download free 30-day trial

How to Update the Custom UserControl of the SplashScreen

Environment

Product Version Product Author
2023.1.314 SplashScreen for WinForms Desislava Yordanova

Description

The SplashScreen from the Telerik UI for WinForms suite offers the possibility to show a custom UserControl. This article demonstrates a sample approach how to update the UserControl outside its class.

update-splash-screen-control

Solution

The RadSplashScreenManager internally uses a background Thread for showing the splash. Make sure that you don't access the RadSplashScreenControl or its elements outside the main UI thread. Note that all UI controls are not thread safe controls in the whole Windows Forms platform (not just Telerik controls, but all controls out there). You should use an Invoke to update the controls in cross threading scenario.


  internal static class Program
{
    private static Form1? _mainForm;
    private static int _currentProgress=0;
    private static SplashUserControl uc;

    /// <summary>
    ///  The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        // To customize application configuration such as set high DPI settings or default font,
        // see https://aka.ms/applicationconfiguration.
        ApplicationConfiguration.Initialize();

        RadSplashScreenManager.ContentCreated += RadSplashScreenManager_ContentCreated; 
        RadSplashScreenManager.FormLoad += RadSplashScreenManager_FormLoad;

        if (Screen.PrimaryScreen != null)
        { 
            RadSplashScreenManager.Show(typeof(SplashUserControl)); 
        } 
        _mainForm = new Form1(); 
        UpdateSplash("Logging Initialized"); 
        UpdateSplash("Program Setup Finished"); 
        UpdateSplash("Splash Started"); 
        Application.Run(_mainForm);
    }

    private static void RadSplashScreenManager_FormLoad(SplashFormEventArgs e)
    {
        UpdateSplash("Splash Started"); 
    } 

    private static void RadSplashScreenManager_ContentCreated(ContentCreatedEventArgs e)
    {
         uc = e.Content as SplashUserControl;
        _currentProgress = Math.Min(100, _currentProgress + 10); 
    }

    public static void UpdateSplash(string milestone = "")
    {
        _currentProgress = Math.Min(100, _currentProgress + 10);

        Action action = new Action(() =>
        {
            if (uc!=null)
            {
                uc.progressBar.Value1 = _currentProgress;
                uc.textBox.Text = "Loading... " + _currentProgress + "% " + milestone;

            }
        });

        uc.Invoke(action);
        Thread.Sleep(1500);
    }

    public static bool CloseSplash()
    {
        RadSplashScreenManager.ContentCreated -= RadSplashScreenManager_ContentCreated;
        RadSplashScreenManager.FormLoad -= RadSplashScreenManager_FormLoad; 
        RadSplashScreenManager.Close();
        return false;
    }      
}    

public partial class Form1 : RadForm
{ 
    public Form1()
    { 
        InitializeComponent();

        this.Load += Form1_Load;
        Program.UpdateSplash("1. Building Main Form");
        Program.UpdateSplash("2. Main Form Built");  
        Program.UpdateSplash("3. Connecting to DB");   
        Program.UpdateSplash("4. Connected to DB"); 

    }

    private void Form1_Load(object? sender, EventArgs e)
    {
        Program.UpdateSplash("5. Main Form Finalization"); 

        Program.CloseSplash();
    }
}