I made a simple WindowsFormsApplication but I have some difficulties with it. In the form I have 10 TextBoxes and a button. The purpose of the program is to generate a different number in every single box when I click the button. Here is a part from the code:
private void button1_Click(object sender, EventArgs e)
{
int p = 0;
int[] array = GeneratingArray();
foreach (Control c in tableLayoutPanel1.Controls)
{
if (c.GetType().ToString() == "System.Windows.Forms.TextBox")
{
c.Text = array[p].ToString();
p++;
}
}
}
public int GeneratingInt()
{
int random;
Contract.Ensures(Contract.Result<int>() > -11, "Array out of Bounds(-10 ; 10)");
Contract.Ensures(Contract.Result<int>() < 11, "Array out of Bounds(-10 ; 10)");
Random gnr = new Random();
random = gnr.Next(20);
return random;
}
public int[] GeneratingArray()
{
int[] array = new int[10];
int random;
for (int i = 0; i < 10; i++)
{
random = GeneratingInt();
array[i] = random;
}
return array;
}
The problem is that when I use the debugger all works fine but when I start the application in all the boxes is generated the same number. I can't find what causes this kind of problem so I'm asking you. Thanks.