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:
Using the code
Set all the label locations, font size, and font style as the same.
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)));
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)).
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:
string s = "Animated Label";
string[] l;
int i = 0, j = 0;
Place the following code in the timer control's tick event:
if (i < s.Length-1)
label2.Text += l[i].ToString();
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.