public partial class Form1 : Form
{
private int dx = 50;
private int dy = 50;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Rectangle screen = Screen.FromControl(this).WorkingArea;
int newX = this.Location.X + dx;
int newY = this.Location.Y + dy;
if (newX < screen.Left || newX + this.Width > screen.Right)
{
dx = -dx;
newX = this.Location.X + dx;
}
if (newY < screen.Top || newY + this.Height > screen.Bottom)
{
dy = -dy;
newY = this.Location.Y + dy;
}
this.Location = new Point(newX, newY);
this.Text = $"X = {this.Location.X}, Y = {this.Location.Y}";
}
}