Answers for "how to fade c# form"

C#
0

how to fade c# form

Timer t1 = new Timer();

private void Form1_Load(object sender, EventArgs e)
{
            Opacity = 0;      //first the opacity is 0

            t1.Interval = 10;  //we'll increase the opacity every 10ms
            t1.Tick += new EventHandler(fadeIn);  //this calls the function that changes opacity 
            t1.Start(); 
}

void fadeIn(object sender, EventArgs e)
{
            if (Opacity >= 1)  
                t1.Stop();   //this stops the timer if the form is completely displayed
            else
                Opacity += 0.05;
}
Posted by: Guest on March-25-2022
0

how to fade c# form

private void main_FormClosing(object sender, FormClosingEventArgs e)
{
      e.Cancel = true;    //cancel the event so the form won't be closed

      t1.Tick += new EventHandler(fadeOut);  //this calls the fade out function
      t1.Start();

      if (Opacity == 0)  //if the form is completly transparent
          e.Cancel = false;   //resume the event - the program can be closed

}

void fadeOut(object sender, EventArgs e)
{
      if (Opacity <= 0)     //check if opacity is 0
      {
          t1.Stop();    //if it is, we stop the timer
          Close();   //and we try to close the form
      }
      else
          Opacity -= 0.05;
}
Posted by: Guest on March-25-2022

C# Answers by Framework

Browse Popular Code Answers by Language