Label Effect: Auto Typing Application in C#

Today in C#, I will teach you how to create a program that will have an Auto Typing Application using the Label control. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your project as Label Typing Effect. 2. Next, add a Label named Label1, a Button named Button1 and labeled "Start Typing", and lastly add a Timer named Timer1. You must design your layout like this: design 3. In your code module, initialize this as your Global Variable that has public as access modifier.
  1. public string str_;
  2. public int count_;
4. Next, we will code for our Timer1. Our timer will be our basis for the auto typing. Put this code below:
  1. public void Timer1_Tick(object sender, EventArgs e)
  2.                 {
  3.                         if (Label1.Text.Length == str_.Length)
  4.                         {
  5.                                 Timer1.Enabled = false;
  6.                                 return;
  7.                         }
  8.                         Label1.Text = str_.Substring(0, count_);
  9.                         count_++;
  10.                 }
If the length of our label1 is the same with the length we inputted in variable str, then the timer will be stopped. Otherwise if not, the timer will continue to tick and every 1 ms tick of the Timer it will display the desired text in Label1 as we have the SubString function of the str variable. 5. Lastly, put this code for Button1.
  1. public void Button1_Click(object sender, EventArgs e)
  2.                 {
  3.                         Label1.Text = "";
  4.                         count_ = 1;
  5.                         str_ = "Sourcecodester is the best!";
  6.                         Timer1.Enabled = true;
  7.                 }
When button1 is clicked the label will started to be in "blank" and count length will be equal to 1, meaning the letter will appear with one by one character. The string to be displayed "Sourcecodester is the best!" and the timer will start. When the timer start, the substring will be incremented by one so that it will have an automatic typing of the every single character. Full source code:
  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Collections.Generic;
  8.  
  9.  
  10.  
  11. namespace Label_Typing_Effect
  12. {
  13.         public partial class Form1
  14.         {
  15.                 public Form1()
  16.                 {
  17.                         InitializeComponent();
  18.                        
  19.        
  20.                 }
  21.                
  22.                 public string str_;
  23.                 public int count_;
  24.                
  25.                 public void Timer1_Tick(object sender, EventArgs e)
  26.                 {
  27.                         if (Label1.Text.Length == str_.Length)
  28.                         {
  29.                                 Timer1.Enabled = false;
  30.                                 return;
  31.                         }
  32.                         Label1.Text = str_.Substring(0, count_);
  33.                         count_++;
  34.                 }
  35.                
  36.                 public void Button1_Click(object sender, EventArgs e)
  37.                 {
  38.                         Label1.Text = "";
  39.                         count_ = 1;
  40.                         str_ = "Sourcecodester is the best!";
  41.                         Timer1.Enabled = true;
  42.                 }
  43.         }
  44. }
Output: output output Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment