Safiron
Safiron

Reputation: 883

C# - Move form after its loaded

I have two forms. In a primary form I have a button wich calls a constructor of another form. After form is loaded I need it to automaticky call a function which will move this form in some direction. I'm having a hard time to find event which will do the work because events like Load or Shown are performed before form is actualy "shown"... That means loop which is moving the form is finished sooner than the form apears and the form is shown in final position. Does anyone know event trigerred after form is visible ? or know another solution to this problem ?

On another topic I've read about Shown event but unfortunatly its acting same as Load event and I dont understand why :( There is just one solution I keep in mind its create second thread with 500ms delay (which should be enough to load form) which will start move() metod but I dont know how to call function created on different thread :( ... thread would end simultaneously with move() metod.

Second form named Title:

public partial class Title : Form
{

    public Title()
    {
        InitializeComponent();
        this.Left = (Screen.AllScreens[0].Bounds.Width + Screen.AllScreens[1].Bounds.Width / 2 - this.Width / 2);
        this.Top = (Screen.AllScreens[1].Bounds.Height-80);        


    }

    // metod moves form
    public void move() 
    {

        while (this.Top > 400)
        {
            this.Top--;
            Thread.Sleep(1);
        }
    }

    // another button closes this form with reverse move animation - this works fine
    public void Destruct()  
    {
        while (this.Top < (Screen.AllScreens[1].Bounds.Height - 80))
        {
            this.Top++;
            Thread.Sleep(1);

        }
        this.Close();
    }

    private void Title_Shown(object sender, EventArgs e)
    {
        move();
    }


}

[SOLUTION] I have used Windows.Timer instead of Thread.Sleep() in loop.

Upvotes: 1

Views: 1002

Answers (1)

George Mamaladze
George Mamaladze

Reputation: 7931

It seems you are going to create some kind of slow fly in effect. I would suggest you use a timer instead of loop with Thread.Sleep(). You can create and enable timer in your event. After every tick of the timer you move the window a little bit. After it reaches the end position you just disable timer (Enabled=false). You can use timer control from toolbox.

Upvotes: 2

Related Questions