I have c# form1 with random numbers created and show those numbers in form2, and I again create new random numbers in form1, and when I try to show form2 for the secnd time I have seen the first time created numbers not the second time ( the data in form2 are not changed). I would appreciate If some one can help. The code for form1 and form2 are:
//form1
public static int var2;
Form secondForm = new Form2();
private void Form1_Load(object sender, EventArgs e)
{
var2 = RandomNumber(1, 50);
secondForm.Show();
secondForm.Refresh();
Thread.Sleep(2000);
secondForm.Hide();
var2 = RandomNumber(1, 50);
secondForm.Show();
secondForm.Refresh();
}
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
//form2
private void Form2_Load(object sender, EventArgs e)
{
this.Invoke(new EventHandler(DisplayText1));
}
private void DisplayText1(object sender, EventArgs e)
{
textBox1.AppendText(" ");
textBox1.AppendText(Form1.var2.ToString());
}
You are reusing the same form when you do the 2nd “secondForm.Show();”. When you call Hide/Show all you are doing is making the form visible/invisible. To confirm this, try setting a breakpoint in Form2_Load, and see how many times it is hit.
If you put the following:
After the 2nd call to RandomNumber(1,50), you will get different #s.
Hope this helps,
John