Winforms Slide

I am making a form at the bottom of the screen and I want it to move up, 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 goes through and shows the end position, without showing the shape sliding in which I want. I tried Refresh, DoEvents - any thoughts?

+4
source share
2 answers

Run the code in the 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(); 
+2
source

Try using the Timer event instead of a loop.

+6
source

Source: https://habr.com/ru/post/1304923/


All Articles