You don't need P/Invoke! This is bad, kills multiplatform compatibility.
Better keep using pure .NET, but
do it properly. This is complete source code:
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace SlideForm {
public partial class MyForm : Form {
const int Step = 1;
const int Steps = 500;
const int DelayMs = 20;
const string buttonName = "&Animate";
public MyForm() {
Button button = new Button();
button.Text = buttonName;
this.Controls.Add(button);
button.Click += (sender, eventArgs) => { StartAnimation(); }
}
void StartAnimation() {
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (sender, eventArgs) => {
for (int count = 0; count < Steps; ++count) {
this.Invoke(new Action<Form>((Form form) => {
form.Left += Step;
}), this);
System.Threading.Thread.Sleep(DelayMs);
}
};
worker.RunWorkerAsync();
}
}
}
I developed the code in few minutes for you. It works, tested.
For the purpose of having all code in one file (except entry point which is the same as auto-generated when a project is created from template), the designer is not used. In many cases, this is the best way do to Forms development. ;-)
Animation works, but there is something else to do: 1) prevent button event when animation is in progress; 2) abort animation when a form is being closed. Those are small things, I would leave it for your home exercise.
Good luck,
—SA