# VBForums CodeBank > CodeBank - C# >  Nice fading in and out form effect

## samtheman

Hi,

A while ago I designed a small subroutine that would allow me to fade forms in and out. The effect is very nice and adds a bit of sleekness to your application. There is also a VB.NET version, this can be found in the VB.net codebank here.



```
public void FormFade(object FType) 
{ 
    switch (FType) { 
        case ("in"): 
            int FadeCount = 0; 
            for (FadeCount = 10; FadeCount <= 90; FadeCount += 10) { 
                this.Opacity = FadeCount / 100; 
                this.Refresh(); 
                Threading.Thread.Sleep(50); 
            } 

            break; 
        case ("out"): 
            int FadeCount = 0; 
            for (FadeCount = 90; FadeCount >= 10; FadeCount += -10) { 
                this.Opacity = FadeCount / 100; 
                this.Refresh(); 
                Threading.Thread.Sleep(50); 
            } 

            break; 
    } 
    this.Opacity = 99; 
    
}
```

This subroutine can be called like this:

FormFade("in") for fading your form in
FormFade("out") for fading your form out

The form fade in can be implemented on your Form Load event handler like so:



```
     private void Form1_Load(object sender, EventArgs e)
        {
         FormFade("in")
        }
```

The form fade out can be implemented on your Form Closing event handler as it will catch all attempts to close, not just ones you might add to your Command Button. This can be implemented like this:



```
private void Form1_Closing(object sender, EventArgs e);
{ 
    FormFade("out"); 
}
```

*Additional Information*
This should work with all versions of .net

As you have seen I have set This.Opacity = 99. This is for two reasons. One being that the fade in effect does not fully reach the 100/99% so the form does not look solid. The second is that if fading out with 100% opacity the form will glitch slightly. You will notice no difference between 99% and 100% opacity though.

I'd love to know what you think of this, yes it's not original, but for those looking for something like this, I think it is a good little script.

----------

