-1

Each time I buttonclick my button, I would like my loop to execute and each time a single, random day of the week appears in my textBox1. For example, buttonclick-Tuesday, buttonclick-Thursday, buttonclick-Monday, buttonclick-Friday. I need the loop to execute a total of four (4) times. Then close the form.

for (int i = 0; i <= 4; i++)
{
  String[] strpleaseloop =    { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
  Random r = new Random();
  int iSelect = r.Next(0, 6);
  textBox1.Text = strpleaseloop[iSelect];
  this.Close();
}
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156

1 Answers1

0

If I understand correctly, you should not use a loop. Instead, use a counter. This should work fine.

Outside the button click:

int count = 0;

Inside the button click:

if (count == 4) this.Close();
count++;
String[] strpleaseloop =    { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
Random r = new Random();
int iSelect = r.Next(0, 6);
textBox1.Text = strpleaseloop[iSelect];

Also, I strongly recommend you to put the Random r = new Random(); outside of the button click, because recreating the Random class can cause problems in some specific situations. Read about it here.

Guilherme
  • 5,143
  • 5
  • 39
  • 60
  • Guilherme, thanks but every time I click my button I cannot get the program to stop at 4. I'm new to programming. What do you mean when you say "Outside the button click". – Mr. Marlee Oct 10 '17 at 01:18