我用c#form1創建了隨機數並在form2中顯示這些數字,並且我再次在form1中創建了新的隨機數,並且當我嘗試在secnd時間顯示form2時,我已經看到第一個不是第二次創建數字(form2中的數據不會更改)。如果有人能提供幫助,我將不勝感激。爲Form 1和Form中的代碼是:從form1中加載form2很多次
//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());
}
的WinForms或WPF? – Yuck