Click here to Skip to main content
15,891,976 members
Articles / Programming Languages / C#
Tip/Trick

Animated Label in C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
11 Mar 2013CPOL1 min read 34.6K   3.6K   7   1
This is an animated label in C#.

Sample Image

Introduction

This article explains how to create an animated label in C#. First when I started thinking about creating an animated label in C#, I was confused but finally I got an idea, it's very simple to create, the only logic is we have to take three Label controls to create it. Each Label is placed on another Label, i.e., Label2 is placed on Label1, and Label3 is placed on Label2. Let's take a look at the following image:

Image 2

Using the code

Set all the label locations, font size, and font style as the same.

C#
label1.Location = label2.Location = label3.Location = new Point(50, 30);
label1.Font = label2.Font = label3.Font = new System.Drawing.Font("Microsoft Sans Serif",
        24F,
        System.Drawing.FontStyle.Bold,
        System.Drawing.GraphicsUnit.Point,
        ((byte)(0)));
//
//giving different color to the middile label(label2)
//
this.label2.ForeColor = System.Drawing.Color.Blue;

Write the above code in the Form_Load event. By giving a different colour to the middle label (label2) the animation will appear. Coming to the animation part, the first label is fixed, it will not move and the text of this label will not change. label2 and label3 do not contain any text, but label2.text will start adding characters (letters, i.e., 'A,n,i,m,a,t,e,d, ,L,a,b,e,l') on the Form1_load event (label2.color is blue). label3 stars adding text to it after 3 letters, adding the label2 (here label1 and label 3 colors are same (i.e., black)).

Image 3

Comes to the design part, add three Label controls to the form and also add the timer control to the form in the coding part. First take some variables:

C#
string s = "Animated Label";
string[] l;
int i = 0, j = 0;

Place the following code in the timer control's tick event:

C#
//adding the characters one by one to the label2
if (i < s.Length-1)
    label2.Text += l[i].ToString();

//starting the third label after 3 charaters of label2 adding
if (i >=3 && i <= 20)
{
    if (i <=s.Length+2)
        label3.Text += l[j].ToString();
    j++;
}
if (j <=s.Length)
    i++;
else
{
    i = j = 0;
    label3.Text = label2.Text = "";
}

History

  • 11 March 2013: First version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
nandixxp17-Mar-13 2:07
nandixxp17-Mar-13 2:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.