How To Ideas | How To Articles | How To Tutorials


2

How To Fade Out Windows Form Before Closing


You want your form to fade out before it gets closed when user clicks on close button.  You can achieve this using Timer control and every time timer ticks, you have to decrease the opacity of your form just a bit.

Instruction:

  1. In your form, insert new Timer control from toolbox. You will see timer1 control at the lower bar as i have in the following image.
    image
  2. Now double click on Form, which will take you to the form_load event, add the following code there. Here we are telling .NET to disable the timer, so the timer will not tick and we are setting its tick interval to 200 millisecond. So, when we enable this timer, it will tick after every 200 millisecond.

    timer1.Enabled = false;
    timer1.Interval = 200;
  3. Now select the timer from the designer and then open its Properties by pressing F4, now go to its events and then double click on Tick event, which will cerate an event for timer tick. And this event will occur every 200 millisecond when the timer is enabled, but currently the timer is disabled. So, the code we write there won’t work for now, but will work when we enable the timer.
    image
  4. Now in the timer1_Tick event, add the following code. This code will decrease the opacity of form by 0.05 every time the timer ticks. and when the opacity of form decreases to less than 0.05, we will close the form.

    if (this.Opacity > 0.05)
    {
         this.Opacity -= 0.05;
    }
    else
    {
         timer1.Enabled = false;
         this.Close();
    }

  5. We have written code to decrease the opacity of form by 0.05 after every 200 millisecond when the timer ticks, but this will happen only if the timer will be enabled, which is currently disabled. So, now we just have to enable the Timer, but where should we enable it. And the answer is: when user tries to close the form… So, when user click on the close button, we enable the timer, which will tick after every 200 millisecond and opacity of our form will decrease by 0.05 every time till it has opacity more than 0.05, after which we finally close the form.
  6. Now Select the Form and then open its Properties by pressing F4, now go to its events and then double click on Form Closing event there, which will create an event for Form Closing in the code file.
    image
  7. In the Form closing event, add the following code. This code will enable the timer, if opacity of form is more than 0.05 and will cancel the closing event. But when the opacity of form decreases to less than 0.05 this code will not work and our form will get closed.

    if (this.Opacity > 0.05)
    {
         timer1.Enabled = true;
         e.Cancel = true;
    }

Filed in: C# Tags: , , , , , , , , , , , , , , , , , , , , , , ,

2 Responses to "How To Fade Out Windows Form Before Closing"

  1. yk says:

    Works great!

    Cheers

  2. bahare says:

    thanks…

Leave a Reply

Submit Comment



© 2013 How To Ideas. All rights reserved.
Proudly designed by Theme Junkie.