Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Make a WindowsForm fade in on form load and fade out on form close

, 12 Apr 2011
Rate this:
Please Sign up or sign in to vote.
using System;using System.Collections.Generic;using System.ComponentModel;using System.Text;using System.Windows.Forms;namespace FrmFadeApp{ public partial class FrmSplashScreen : Form { public frmSplashScreen() { InitializeComponent(); ...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
 
namespace FrmFadeApp
{
    public partial class FrmSplashScreen : Form
    {
        public frmSplashScreen()
        {
            InitializeComponent();
        }
      //Variable used to control whether form fade in or fade out 
        private bool Checkinout;
     
  //override the onload procedure of the from
     protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!DesignMode)
            {
                Checkinout = true;
                Opacity = 0;
                timer1.Enabled = true;
            }
        }
   /*DesignMode basically gets the value which indicates whether the   System.ComponentModel.Component is currently in design mode or not*/
 
// override the OnClosing procedure of the from
   protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            if (e.Cancel == true)
                return;
            if (Opacity > 0)
            {
                Checkinout = false;
                timer1.Enabled = true;
                e.Cancel = true;
            }
        }
   
//On timer1_Tick event use the Given Code 
  private void timer1_Tick(object sender, System.EventArgs e)
        {
            if (Checkinout == false)
            {
                Opacity -= (timer1.Interval / 1000.0);
                if (this.Opacity > 0)
                    timer1.Enabled = true;
                else
                {
                    timer1.Enabled = false;
                    Close();
                }
            }
            else
            {
                Opacity += (timer1.Interval / 1000.0);
                timer1.Enabled = (Opacity < 1.0);
                Checkinout = (Opacity < 1.0);
            }
        }
 
Now Run the Application you will see its FadeIn when Form loads and Fadeout when Form closing.

License

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

About the Author

RaviRanjankr
Software Developer
India India
Microsoft Student Partner | CS Student | MCTS | CP MVP | Crazy Learner | Dreamer
 
An Indian, who loves his country, believes in freedom, He is an enthusiast Techie and crazy learner. He is passionate about Technologies and social media. He holds bachelor degree of CS in Information Technology and now pursuing Master degree in Computer Application.
 
He always excited and keen Interested in learning and sharing knowledge. He loves to write blog, learn new things, listen music, taking arts and Playing Games..
 
He keep himself on the desk of his imagination, hanging around with some inceptions.
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralRe: Code doesn't work. I'm submitting an alternative. PinmemberErion Pici18-Apr-11 23:00 
GeneralRe: I used it and it work just fine, thank you! Pinmembermehran_o_net18-Apr-11 22:50 
Generalyea same problem i ran into thats why i posted above. What s... Pinmembercharles henington19-Apr-11 11:30 
GeneralReason for my vote of 4 It's a good alternate. In fact the c... PinmemberThiago Marçal19-Apr-11 7:42 
GeneralReason for my vote of 2 Code doesn't work. See alternate 2 PinmemberErion Pici18-Apr-11 23:09 
GeneralReason for my vote of 1 reason stated above Pinmembercharles henington14-Apr-11 4:00 
GeneralRe: Check my reply which is too stated above. PinmvpRaviRanjankr14-Apr-11 5:25 
Generalwhy would you base the opacity on the timer1.Interval? thats... Pinmembercharles henington12-Apr-11 16:06 
GeneralRe: Its run properly even doing fade in or Fadeout too. and its ... PinmvpRaviRanjankr14-Apr-11 5:24 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 12 Apr 2011
Article Copyright 2011 by RaviRanjankr
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid