Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Add Fade-In and Fade-Out Effect to Your Windows Form

Author: DevASP
Download Source Code : 492_XPFadeinFadeout.zip

In this article I will try to explain you, how you can give fade-in and fade-out effect to you windows form using C#.

Open visual studio.net and create new application using C#. Drag a button on from1. Add another from in the project i.e. from2 and drag a timer control on it. This timer is used to show the fading effect of the from. Now write the following code on form1 load event:

 

private void Form1_Load(object sender, System.EventArgs e)

{

button1.Text = "Show Style";

}

 

Write the following code on the button1 click event

 

private void button1_Click(object sender, System.EventArgs e)

{

Form2 frm = new Form2();

            frm.ShowDialog(this);

}

 

Now in from2 create a following global variable of Boolean type which basically used to control whether the form fades in or out of view.

 

private bool Checkinout;

 

now we have to override the onload procedure of the from and write the following code for it

 

Opacity is basically the property of the System.Windows.Forms.Form class and use to specify a transparency level for a form and its controls.

 

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

 

we have to override the OnClosing procedure of the from also so write the following code for it.

 

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;

            }

}

 

 

Now double click on the timer control and write the following code on timer1 Tick event

 

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

            }

}

 

 

Article Comments
Hi, thanks for the example!!!
I'm looking for this!!!
From Uruguay!!!
See you!!!

Posted on 6/20/2008 8:32:19 PM by Carlos

Very nice. Works precisely as you describe. Took a bit of head-scratching to realize that the OnLoad and OnClosing code goes in Form2. But quickly solved.

Thanks for the good work. Now I have to figure out how to make this work with text in a text box. But you've given me the basics.

Cordially,

Robert

Posted on 11/14/2008 11:00:20 AM by Robert

Good job!

Thanks for sharing... That's what I needed...

Posted on 11/22/2009 11:09:20 AM by Martin King

Hi, thanks for the example!!!
I'm looking for this!!!
From Uruguay!!!
See you!!!

Posted on 5/19/2010 10:58:10 PM by sankar

thanks.. this codes helps a lot :)

Posted on 7/1/2010 5:44:37 AM by chele

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Moving a Form by clicking anywhere Using Interop and C#

Disclaimer - Privacy
© 2002-2017 DevASP.net