Click here to Skip to main content
11,583,865 members (63,523 online)
Click here to Skip to main content

Tagged as

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

, 12 Apr 2011 CPOL 4.9K 6
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)

Share

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.

You may also be interested in...

Comments and Discussions

 
GeneralRe: Code doesn't work. I'm submitting an alternative. Pin
Erion Pici18-Apr-11 23:00
memberErion Pici18-Apr-11 23:00 
GeneralRe: I used it and it work just fine, thank you! Pin
mehran_o_net18-Apr-11 22:50
membermehran_o_net18-Apr-11 22:50 
Generalyea same problem i ran into thats why i posted above. What s... Pin
charles henington19-Apr-11 11:30
membercharles henington19-Apr-11 11:30 
GeneralReason for my vote of 4 It's a good alternate. In fact the c... Pin
Thiago Marçal19-Apr-11 7:42
memberThiago Marçal19-Apr-11 7:42 
GeneralReason for my vote of 2 Code doesn't work. See alternate 2 Pin
Erion Pici18-Apr-11 23:09
memberErion Pici18-Apr-11 23:09 
GeneralReason for my vote of 1 reason stated above Pin
charles henington14-Apr-11 4:00
membercharles henington14-Apr-11 4:00 
GeneralRe: Check my reply which is too stated above. Pin
RaviRanjankr14-Apr-11 5:25
mvpRaviRanjankr14-Apr-11 5:25 
Check my reply which is too stated above.
Generalwhy would you base the opacity on the timer1.Interval? thats... Pin
charles henington12-Apr-11 16:06
membercharles henington12-Apr-11 16:06 
GeneralRe: Its run properly even doing fade in or Fadeout too. and its ... Pin
RaviRanjankr14-Apr-11 5:24
mvpRaviRanjankr14-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 | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 12 Apr 2011
Article Copyright 2011 by RaviRanjankr
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid