Click here to Skip to main content
Licence CPOL
First Posted 2 Sep 2008
Views 11,542
Downloads 212
Bookmarked 10 times

Opacity of Forms in C#

By Mohammad Dayyan | 2 Sep 2008
Learn more about changing the opacity of forms, using C#.
4 votes, 57.1%
1

2

3
1 vote, 14.3%
4
2 votes, 28.6%
5
2.69/5 - 7 votes
μ 2.69, σa 3.36 [?]

Introduction

In C#, we can access the opacity of a Windows Form using the Opacity property. This article shows you how we can change it, using a simple project.

Using the code

System.Windows.Forms.Timer

We'll try to implement a fade-in and fade-out effect on our WinForm. To do that, we use System.Windows.Forms.Timer.

We will use three Timers in our project:

  • TimerFadein: Shows the fade-in effect. TimerFadein runs the following method on 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 the fade-out effect. TimerFadeout runs the method below on 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 a 0.5 opacity and a fade-out effect. timerHalfFadeOut runs the method below on 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 the WinApp with a fade-in effect

To show our app with a fade-in effect, we must change the opacity of our Form to zero in the constructor. We must enable TimerFadein too, to show our WinApp with a fade-in effect.

public Form1()
{
    InitializeComponent();
    this.Opacity = 0;
    TimerFadein.Enabled = true;
}

Well, other things (like closing the WinApp and the fade-out effect) are very similar to the above method. We only need to enable or disable the timers. See the source code for more details.

Good luck!

License

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

About the Author

Mohammad Dayyan



Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralA common mistake PinmemberScott Bruno7:16 2 Sep '08  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 2 Sep 2008
Article Copyright 2008 by Mohammad Dayyan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid