tags:

views:

87

answers:

3

Hi, I'm making a form at the bottom of the screen and I want it to slide upwards so I wrote the following code:

int destinationX = (Screen.PrimaryScreen.WorkingArea.Width / 2) - (this.Width / 2);
int destinationY = Screen.PrimaryScreen.WorkingArea.Height - this.Height;

this.Location = new Point(destinationX, destinationY + this.Height);

while (this.Location != new Point(destinationX, destinationY))
{
    this.Location = new Point(destinationX, this.Location.Y - 1);
    System.Threading.Thread.Sleep(100);
}

but the code just runs through and shows the end position without showing the form sliding in which is what I want. I've tried Refresh, DoEvents - any thoughts?

+5  A: 

Try using a Timer event instead of a loop.

David
Kinda hacky but I'll give it a try :)
rs
@rs - not hacky, it's the only good way. I'm surprised `DoEvents` didn't work, but really that's a good thing because `DoEvents` is seriously hacky.
Daniel Earwicker
A: 

Run the code in a background thread. Example:

        int destinationX = (Screen.PrimaryScreen.WorkingArea.Width / 2) - (this.Width / 2);
        int destinationY = Screen.PrimaryScreen.WorkingArea.Height - this.Height;

        Point newLocation = new Point(destinationX, destinationY + this.Height);

        new Thread(new ThreadStart(() =>
        {
            do
            {
                 // this line needs to be executed in the UI thread, hence we use Invoke
                this.Invoke(new Action(() => { this.Location = newLocation; }));

                newLocation = new Point(destinationX, newLocation.Y - 1);
                Thread.Sleep(100);
            }
            while (newLocation != new Point(destinationX, destinationY));
        })).Start();
Heinzi
Worked perfectly - thank you :D
rs
A: 

I wrote a MSN-Style popup control in Genghis called AniForm that does just what you want as far as animation goes. Checkout the CodePlex link for the source.

mjmarsh