I'm writing code for a forms application where whenever you mouse over the exit button, a duck quacks at you, and it makes another exit button in a random location that does the same thing. However, whenever I generate a new exit button they follow a downward trend from the top left of the form to the bottom right. The Diagonal Exit Buttons
public Form1()
{
InitializeComponent();
}
private void buttonExit_MouseEnter(object sender, EventArgs e)
{
int x = 745;
int y = 475;
Random randx = new Random();
Random randy = new Random();
Button newButton = new Button();
this.Controls.Add(newButton);
newButton.Location = new Point(randx.Next(x), randy.Next(y));
newButton.Size = new Size(88, 37);
newButton.MouseEnter += NewButton_MouseEnter;
newButton.Text = ("Exit");
buttonExit.Location = new Point(randx.Next(x), randy.Next(y));
SoundPlayer simpleSound = new SoundPlayer(@"C:\Users\Michael\Documents\duck_quack.wav");
simpleSound.Play();
}
private void NewButton_MouseEnter(object sender, EventArgs e)
{
int x = 745;
int y = 475;
Random randx = new Random();
Random randy = new Random();
Button newButton = new Button();
this.Controls.Add(newButton);
newButton.Location = new Point(randx.Next(x), randy.Next(y));
newButton.Size = new Size(88, 37);
newButton.MouseEnter += NewButton_MouseEnter;
newButton.Text = ("Exit");
buttonExit.Location = new Point(randx.Next(x), randy.Next(y));
SoundPlayer simpleSound = new SoundPlayer(@"C:\Users\Michael\Documents\duck_quack.wav");
simpleSound.Play();
}
private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
}
What do I seem to be doing wrong? Why are the buttons generating like that? How can I write it so that the buttons generate randomly across the form?