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

Opacity of Forms in C#

0.00/5 (No votes)
2 Sep 2008 2  
Learn more about Opacity in C#

Introduction

In C# we can access to opacity of WinForm by Opacity property .
This article shows you how we can do it by a simple project.

Using the code

System.Windows.Forms.Timer

We'd like to make fade-in and fade-out effect in our WinForm.
To do that we used System.Windows.Forms.Timer .

We used three Timers in our project :
TimerFadein : Shows WinApp with fade-in effect.
TimerFadein runs below method in its Tick event.
private void TimerFadein_Tick(object sender, EventArgs e)
{
    //Prevents Timers overlapping 
    if (timerHalfFadeOut.Enabled || TimerFadeout.Enabled)
    {
        TimerFadein.Enabled = false;
        return;
    }
    timerRunning = true;
    //\\
    this.Opacity += 0.05;
    if (this.Opacity >= 0.95)
    {
        this.Opacity = 1;
        timerRunning = TimerFadein.Enabled = false;
    }
    maskedTextBoxOpacity.Text = (this.Opacity * 100).ToString();
    hScrollBar1.Value = (int)(this.Opacity * 100);
}
TimerFadeout : Shows WinApp with fade-out effect.
TimerFadeout runs below method in its Tick event.
private void TimerFadeout_Tick(object sender, EventArgs e)
{
    //Prevents Timers overlapping
    if (timerHalfFadeOut.Enabled || TimerFadein.Enabled)
    {
        TimerFadeout.Enabled = false;
        return;
    }
    timerRunning = true;
    //\\
    this.Opacity -= 0.05;
    if (this.Opacity <= 0.05)
    {
        this.Opacity = 0;
        Application.ExitThread();
    }
    maskedTextBoxOpacity.Text = (this.Opacity * 100).ToString();
    hScrollBar1.Value = (int)(this.Opacity * 100);
}
timerHalfFadeOut : Shows WinApp with 0.5 opacity and fade-out effect.
timerHalfFadeOut runs below method in its Tick event.
private void timerHalfFadeOut_Tick(object sender, EventArgs e)
{
    //Prevents Timers overlapping
    if (TimerFadeout.Enabled || TimerFadein.Enabled)
    {
        timerHalfFadeOut.Enabled = false;
        return;
    }
    timerRunning = true;
    //\\
    this.Opacity -= 0.05;
    if (this.Opacity <= 0.50)
    {
        this.Opacity = 0.5;
        timerRunning = timerHalfFadeOut.Enabled = false;
    }
    maskedTextBoxOpacity.Text = (this.Opacity * 100).ToString();
    hScrollBar1.Value = (int)(this.Opacity * 100);
}

Running WinApp with Fade-in effect

We'd like to show our project with fade-in effect thus we must change opacity of our Form to zero in constructor method .
We must enable TimerFadein too , to show our WinApp with fade-in effect.
public Form1()
{
    InitializeComponent();
    this.Opacity = 0;
    TimerFadein.Enabled = true;
}
Well, other things (like closing WinApp and fade-out effect) are very similar with above method .
Only we must enable or disable Timers.
See source code.
Good luck

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