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

Simple Animation for your Windows Forms or Controls using C#.NET

Rate me:
Please Sign up or sign in to vote.
4.86/5 (16 votes)
11 Nov 2014CPOL 76.5K   5.2K   13   24
Simple Animation with Timer control in C#.NET (Sliding and Scrolling Effects)

Introduction

There are a lot of techniques for bringing animation in your Windows application - simple and complex techniques. Here, I can explain a simple technique for giving animation or moving effects for your Windows Forms or any other controls.

Using the Code

Follow these steps:

  1. Create a solution (e.g.: SimpleAnimation)
  2. Place your controls on it:
    e.g.:
    • Split Container - scnrMain
    • Buttons - btnAnimation1, btnAnimation2, btnReset, btnQuit
    • Panel - pnlMain
  3. Add a timer control (e.g.: tmrMain)
  4. Write your code as follows:
    C#
    //frmMain
    // 
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace SimpleAnimation
    {
        public partial class frmMain : Form
        {
    //
    //declares variables used in this class
    //
            int scnrWidth;
            int scnrHeight;
            int pnlWidth;
            int pnlHeight;
    
            public frmMain()
            {
                InitializeComponent();
            }
    //
    //allocate values to variables on Load event
    //
            public void frmMain_Load(object sender, EventArgs e)
            {
                pnlMain.Width = 0;
                pnlMain.Height = 0;
    
                scnrWidth = scnrMain.Panel2.Width;
                scnrHeight = scnrMain.Panel2.Height;
                pnlWidth = pnlMain.Width;
                pnlHeight = pnlMain.Height;
            }
    //
    //timer control activated on click event of button Animation 1 - first option
    //
            private void btnAnimation1_Click(object sender, EventArgs e)
            {
                tmrMain.Enabled = true;
                btnAnimation1.Enabled = false;
                btnAnimation2.Enabled = false;
            }
    //
    //when timer activated, the width and height of panel increased using a while loop
    //
            private void tmrMain_Tick(object sender, EventArgs e)
            {
                while(pnlWidth < scnrWidth || pnlHeight < scnrHeight)
                {
                    if (pnlMain.Width < scnrWidth)
                    {
                        pnlMain.Width = pnlWidth + 1;
                        pnlWidth++;
                    }
                    if (pnlMain.Height < scnrHeight)
                    {
                        pnlMain.Height = pnlHeight + 1;
                        pnlHeight++;
                    }
                    break;
                }
            }
    //
    //this is another way to increase panel width and height using while loop - second option
    //
            private void btnAnimation2_Click(object sender, EventArgs e)
            {
                pnlMain.Height = scnrHeight;
                while (pnlMain.Width < scnrWidth)
                {
                    pnlMain.Width++;
                }
                btnAnimation2.Enabled = false;
                btnAnimation2.Enabled = false;
            }
    //
    //re-allocate values to variables and properties of controls
    //
            private void btnReset_Click(object sender, EventArgs e)
            {
                pnlMain.Width = 0;
                pnlMain.Height = 0;
    
                scnrWidth = scnrMain.Panel2.Width;
                scnrHeight = scnrMain.Panel2.Height;
                pnlWidth = pnlMain.Width;
                pnlHeight = pnlMain.Height;
                btnAnimation1.Enabled = true;
                btnAnimation2.Enabled = true;
                tmrMain.Enabled = false;
            }
    //
    //following lines of code will give your form a 
    //upward scrolling effect - by decreasing height of form
    //
            private void btnQuitApp_Click(object sender, EventArgs e)
            {
                while (this.Height>60)
                {
                    this.Height--;
                }
                Application.Exit();
            }
        }
    }
    
    //End of code. 

The sample project will be as shown in Figure 1:

Figure.1:frmMain

Figure 1

Thank you for using my tip.

Enjoy programming...

License

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


Written By
Software Developer Freelance
India India
He is a Commerce Graduate and working as an Accountant. He learned Programming because of his passion and craze. He is a beginner in .NET. His favourite language is C# and favourite database is MySQL.
He loves playing guitar and listening to music.

Comments and Discussions

 
QuestionDraw a curve and move it in the form Pin
Member 1347566420-Nov-17 1:50
Member 1347566420-Nov-17 1:50 
QuestionInteresting Pin
BotReject14-Nov-14 23:55
BotReject14-Nov-14 23:55 
AnswerRe: Interesting Pin
Noble KC17-Nov-14 19:14
professionalNoble KC17-Nov-14 19:14 
GeneralMy vote of 2 Pin
Anand Pandey13-Nov-14 20:57
Anand Pandey13-Nov-14 20:57 
GeneralRe: My vote of 2 Pin
Noble KC13-Nov-14 23:06
professionalNoble KC13-Nov-14 23:06 
QuestionMy vote of 5! Pin
Volynsky Alex12-Nov-14 4:06
professionalVolynsky Alex12-Nov-14 4:06 
AnswerRe: My vote of 5! Pin
Noble KC12-Nov-14 19:44
professionalNoble KC12-Nov-14 19:44 
GeneralRe: My vote of 5! Pin
Volynsky Alex12-Nov-14 20:30
professionalVolynsky Alex12-Nov-14 20:30 
You're welcome Noble KC
GeneralMy vote of 4 Pin
srilekhamenon12-Nov-14 0:31
professionalsrilekhamenon12-Nov-14 0:31 
GeneralRe: My vote of 4 Pin
Noble KC12-Nov-14 19:48
professionalNoble KC12-Nov-14 19:48 
QuestionNote: picture is missing from your Tip Pin
BillWoodruff12-Nov-14 0:06
professionalBillWoodruff12-Nov-14 0:06 
AnswerRe: Note: picture is missing from your Tip Pin
Nelek12-Nov-14 0:10
protectorNelek12-Nov-14 0:10 
GeneralRe: Note: picture is missing from your Tip Pin
Noble KC13-Nov-14 0:36
professionalNoble KC13-Nov-14 0:36 
GeneralMy vote of 1 Pin
CeGu11-Nov-14 20:19
professionalCeGu11-Nov-14 20:19 
GeneralRe: My vote of 1 Pin
Noble KC11-Nov-14 21:10
professionalNoble KC11-Nov-14 21:10 
GeneralRe: My vote of 1 Pin
CeGu5-Dec-14 2:40
professionalCeGu5-Dec-14 2:40 
GeneralMy vote of 1 Pin
Alexander Sharykin11-Nov-14 20:07
Alexander Sharykin11-Nov-14 20:07 
GeneralRe: My vote of 1 Pin
Noble KC11-Nov-14 21:13
professionalNoble KC11-Nov-14 21:13 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun11-Nov-14 19:30
Humayun Kabir Mamun11-Nov-14 19:30 
Questionimage not displayed Pin
deo cabral11-Nov-14 19:15
professionaldeo cabral11-Nov-14 19:15 
AnswerRe: image not displayed Pin
Noble KC11-Nov-14 21:14
professionalNoble KC11-Nov-14 21:14 
GeneralRe: image not displayed Pin
deo cabral12-Nov-14 14:42
professionaldeo cabral12-Nov-14 14:42 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun11-Nov-14 19:04
Humayun Kabir Mamun11-Nov-14 19:04 
GeneralRe: My vote of 5 Pin
Noble KC11-Nov-14 21:15
professionalNoble KC11-Nov-14 21:15 

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.