C# - Simple Splash Screen

In this tutorial we will create a Simple Splash Screen using C#. C# is a general-purpose, object-oriented programming language. C# automatically manages inaccessible object memory using a garbage collector, which eliminates developer concerns and memory leaks. C# is faster than dynamically typed languages because things are more clearly defined. It contains several classes that support any C# platforms, like game development. It has a friendly environment for all new developers. So let's do the coding.

Getting Started

First you will have to download & install the Visual Studio. Visual Studios is an open source development feel free to create any application that you want. Here's the link for the Visual Studio https://www.visualstudio.com/

Application Design

We will now create the design for the application, first locate the designer file called form1.Designer.cs, this is the default name when you create a new windows form. Then write these codes inside your designer file.
  1. namespace Simple_Splash_Screen
  2. {
  3. partial class Form1
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// Clean up any resources being used.
  12. /// </summary>
  13. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22.  
  23. #region Windows Form Designer generated code
  24.  
  25. /// <summary>
  26. /// Required method for Designer support - do not modify
  27. /// the contents of this method with the code editor.
  28. /// </summary>
  29. private void InitializeComponent()
  30. {
  31. this.components = new System.ComponentModel.Container();
  32. this.pb_loading = new System.Windows.Forms.ProgressBar();
  33. this.t_duration = new System.Windows.Forms.Timer(this.components);
  34. this.SuspendLayout();
  35. //
  36. // pb_loading
  37. //
  38. this.pb_loading.Location = new System.Drawing.Point(136, 491);
  39. this.pb_loading.Name = "pb_loading";
  40. this.pb_loading.Size = new System.Drawing.Size(620, 24);
  41. this.pb_loading.TabIndex = 0;
  42. //
  43. // t_duration
  44. //
  45. this.t_duration.Enabled = true;
  46. this.t_duration.Interval = 50;
  47. this.t_duration.Tick += new System.EventHandler(this.StartTimer);
  48. //
  49. // Form1
  50. //
  51. this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
  52. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  53. this.BackgroundImage = global::Simple_Splash_Screen.Properties.Resources._2497315_alien_wallpapers;
  54. this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
  55. this.ClientSize = new System.Drawing.Size(909, 579);
  56. this.Controls.Add(this.pb_loading);
  57. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
  58. this.Name = "Form1";
  59. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  60. this.Text = "Smple Splash Screen";
  61. this.ResumeLayout(false);
  62.  
  63. }
  64.  
  65. #endregion
  66.  
  67. private System.Windows.Forms.ProgressBar pb_loading;
  68. private System.Windows.Forms.Timer t_duration;
  69. }
  70. }
After that create another form by going to the menu Project then select Add Windows Form. Then write these block of codes inside the designer script to render the interface of the form.
  1. namespace Simple_Splash_Screen
  2. {
  3. partial class Form2
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// Clean up any resources being used.
  12. /// </summary>
  13. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22.  
  23. #region Windows Form Designer generated code
  24.  
  25. /// <summary>
  26. /// Required method for Designer support - do not modify
  27. /// the contents of this method with the code editor.
  28. /// </summary>
  29. private void InitializeComponent()
  30. {
  31. this.label1 = new System.Windows.Forms.Label();
  32. this.SuspendLayout();
  33. //
  34. // label1
  35. //
  36. this.label1.AutoSize = true;
  37. this.label1.Font = new System.Drawing.Font("Mistral", 28F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  38. this.label1.Location = new System.Drawing.Point(150, 122);
  39. this.label1.Name = "label1";
  40. this.label1.Size = new System.Drawing.Size(341, 67);
  41. this.label1.TabIndex = 0;
  42. this.label1.Text = "Welcome Human!";
  43. //
  44. // Form2
  45. //
  46. this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
  47. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  48. this.ClientSize = new System.Drawing.Size(682, 466);
  49. this.Controls.Add(this.label1);
  50. this.Name = "Form2";
  51. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  52. this.Text = "Main Form";
  53. this.ResumeLayout(false);
  54. this.PerformLayout();
  55.  
  56. }
  57.  
  58. #endregion
  59.  
  60. private System.Windows.Forms.Label label1;
  61. }
  62. }
or also you create the layout by dragging the proper tools to the forms. Note: To make the progress bar work make sure the you insert the Timer component in the Form by dragging the component to it.

Managing the Main Application Class

This is the main script that runs the whole class of the application. We will add some methods to enhance the capabilities of the existing function of the script. To do the that open Program.cs then write these methods inside the Static void Main()
  1. var startForm = new Form1();
  2. startForm.FormClosed += new FormClosedEventHandler(FormClosed);
  3. startForm.Show();
  4. Application.Run();
Then add a new methods to handle the script that we added and to be able to run it prroperly. Write these code block of codes.
  1. static void FormClosed(object sender, FormClosedEventArgs e) {
  2. ((Form)sender).FormClosed -= FormClosed;
  3. if (Application.OpenForms.Count == 0) Application.ExitThread();
  4. else Application.OpenForms[0].FormClosed += FormClosed;
  5. }
This is the whole code for the Program.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows.Forms;
  6.  
  7. namespace Simple_Splash_Screen
  8. {
  9. static class Program
  10. {
  11. /// <summary>
  12. /// The main entry point for the application.
  13. /// </summary>
  14. [STAThread]
  15. static void Main()
  16. {
  17. Application.EnableVisualStyles();
  18. Application.SetCompatibleTextRenderingDefault(false);
  19. var startForm = new Form1();
  20. startForm.FormClosed += new FormClosedEventHandler(FormClosed);
  21. startForm.Show();
  22. Application.Run();
  23. }
  24.  
  25. static void FormClosed(object sender, FormClosedEventArgs e) {
  26. ((Form)sender).FormClosed -= FormClosed;
  27. if (Application.OpenForms.Count == 0) Application.ExitThread();
  28. else Application.OpenForms[0].FormClosed += FormClosed;
  29. }
  30.  
  31. }
  32. }

Creating the Script

This code contains the function of the application. This code will the event when the application start running.This will run the timer and increment the progress bar each seconds, and when the progress bar reach the maximum value it will open a new form at the same time.To do that locate the csharp script, In my case the default script will be called Form1.cs. Then write these block of codes inside the Class of the form called Form1.
  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.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace Simple_Splash_Screen
  13. {
  14. public partial class Form1 : Form
  15. {
  16. Form2 frm2;
  17.  
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. }
  22.  
  23. private async void Delay() {
  24. await Task.Delay(5000);
  25. frm2 = new Form2();
  26. frm2.Show();
  27. this.Close();
  28. }
  29.  
  30. private void StartTimer(object sender, EventArgs e) {
  31. pb_loading.Maximum = 100;
  32. pb_loading.Increment(1);
  33. if (pb_loading.Value == 100) {
  34. t_duration.Stop();
  35. Delay();
  36. }
  37. }
  38.  
  39.  
  40. }
  41. }
Try to run the application and see if it works. There you go we successfully create a Simple Splash Screen using C#. I hope that this tutorial help you understand on how to develop an application using C#. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment