Click here to Skip to main content
15,170,607 members
Articles / Programming Languages / C#
Tip/Trick
Posted 11 Nov 2014

Stats

65K views
5K downloads
13 bookmarked

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
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)

Share

About the Author

Noble KC
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 2:50
MemberMember 1347566420-Nov-17 2:50 
QuestionInteresting Pin
BotReject15-Nov-14 0:55
MemberBotReject15-Nov-14 0:55 
AnswerRe: Interesting Pin
Noble KC17-Nov-14 20:14
professionalNoble KC17-Nov-14 20:14 
GeneralMy vote of 2 Pin
Anand Pandey13-Nov-14 21:57
MemberAnand Pandey13-Nov-14 21:57 
GeneralRe: My vote of 2 Pin
Noble KC14-Nov-14 0:06
professionalNoble KC14-Nov-14 0:06 
QuestionMy vote of 5! Pin
Volynsky Alex12-Nov-14 5:06
professionalVolynsky Alex12-Nov-14 5:06 
AnswerRe: My vote of 5! Pin
Noble KC12-Nov-14 20:44
professionalNoble KC12-Nov-14 20:44 
GeneralRe: My vote of 5! Pin
Volynsky Alex12-Nov-14 21:30
professionalVolynsky Alex12-Nov-14 21:30 
GeneralMy vote of 4 Pin
srilekhamenon12-Nov-14 1:31
professionalsrilekhamenon12-Nov-14 1:31 
GeneralRe: My vote of 4 Pin
Noble KC12-Nov-14 20:48
professionalNoble KC12-Nov-14 20:48 
QuestionNote: picture is missing from your Tip Pin
BillWoodruff12-Nov-14 1:06
mveBillWoodruff12-Nov-14 1:06 
AnswerRe: Note: picture is missing from your Tip Pin
Nelek12-Nov-14 1:10
protectorNelek12-Nov-14 1:10 
GeneralRe: Note: picture is missing from your Tip Pin
Noble KC13-Nov-14 1:36
professionalNoble KC13-Nov-14 1:36 
GeneralMy vote of 1 Pin
CeGu11-Nov-14 21:19
professionalCeGu11-Nov-14 21:19 
GeneralRe: My vote of 1 Pin
Noble KC11-Nov-14 22:10
professionalNoble KC11-Nov-14 22:10 
GeneralRe: My vote of 1 Pin
CeGu5-Dec-14 3:40
professionalCeGu5-Dec-14 3:40 
GeneralMy vote of 1 Pin
Alexander Sharykin11-Nov-14 21:07
MemberAlexander Sharykin11-Nov-14 21:07 
GeneralRe: My vote of 1 Pin
Noble KC11-Nov-14 22:13
professionalNoble KC11-Nov-14 22:13 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun11-Nov-14 20:30
MemberHumayun Kabir Mamun11-Nov-14 20:30 
Questionimage not displayed Pin
deo cabral11-Nov-14 20:15
professionaldeo cabral11-Nov-14 20:15 
AnswerRe: image not displayed Pin
Noble KC11-Nov-14 22:14
professionalNoble KC11-Nov-14 22:14 
GeneralRe: image not displayed Pin
deo cabral12-Nov-14 15:42
professionaldeo cabral12-Nov-14 15:42 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun11-Nov-14 20:04
MemberHumayun Kabir Mamun11-Nov-14 20:04 
GeneralRe: My vote of 5 Pin
Noble KC11-Nov-14 22:15
professionalNoble KC11-Nov-14 22: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.