解決方案1:您可以int
變量傳遞給新形式Constructor
和分配該值在NewForm
構造函數中。
試試這個:
Program.cs的
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(0));
}
Form 1代碼:
int no = 10;
public Form1(int no)
{
this.no=no;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(no);
this.Hide();
form2.ShowDialog();
}
表2:代碼
int no;
public Form2(int no)
{
this.no = no;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
no = 20;
Form1 form1 = new Form1(no);
this.Hide();
form1.Show();
}
解決方案2:你可以採取public
static
varible在Form1
和訪問使用他們的類名享有一定形式的變量。
試試這個:
Form 1代碼:
public static int no = 10;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
this.Hide();
form2.ShowDialog();
}
表2:代碼
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1.no = 20; //access Form1 int variable
Form1 form1 = new Form1(no);
this.Hide();
form1.Show();
}
請添加代碼的相關章節。 – mostruash