Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
11 Nov 2014 2  
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:
    //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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here