Daniel Daniel - 2 months ago 17
C# Question

Winforms load animation

I've created some code in my

button1_Click
event that adds adds an animation to my WinForms application so it appears in the buttom right corner and slides up from below the taskbar:

Code



private void button1_Click(object sender, EventArgs e)
{
int x = SystemInformation.VirtualScreen.Width/2 - 290;
int y = SystemInformation.VirtualScreen.Height - 90;

Point p = new Point(x, y);
this.Location = p;

for (int i = 0; i < 150; i++)
{
p.Y--;
this.Location = p;
System.Threading.Thread.Sleep(5);
}
}


The result



enter image description here

Now I want my form to open with this animation when created, so I've tried moving my code to the
Form1_Load
event, but for some reason it doesn't work. It just loads normally.

Answer

Move your animation code to the Form.Shown event handler. I would recommend to you still set the initial position in the Form.Load as to avoid a momentary flash of the form at its default position.

Comments