# 複雜的表單間變數

• ### 問題

• public partial class Form1 : Form
{
public string t1,t2,t3;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e) //連到Form2
{
t1 = "11111";
t2 = "22222";
t3 = "33333";
Form2 form2 = new Form2();
form2.form1 = this;
form2.ShowDialog();
this.Close();
}
}

public partial class Form2 : Form
{
internal Form1 form1 = new Form1();
private string tt1, tt2,test1,test2;

public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
tt1 = form1.t1;
tt2 = form1.t2;
textBox1.Text = tt1 + tt2;

textBox2.Text = test1;
textBox3.Text = test2;
}

private void button1_Click(object sender, EventArgs e) //連到Form3
{
test1 = "AAAA";
test2 = "BBBB";
Form3 form3 = new Form3();
form3.form1 = form1; //form1 to form3
form3.ShowDialog();
this.Close();
}
}

public partial class Form3 : Form
{
internal Form1 form1 = new Form1();
private string ttt3;

public Form3()
{
InitializeComponent();
}

private void Form3_Load(object sender, EventArgs e)
{
ttt3 = form1.t3;
textBox1.Text = ttt3;
}

private void button1_Click(object sender, EventArgs e) //連到Form2
{
Form2 form2 = new Form2();
form2.form1 = form1;
form2.ShowDialog();
this.Close();
}
}

以上程式碼可將Form1的變數傳至Form2及Form3，但從Form3連到Form2時tt1、tt2的值還會在，但Form2裡的test1、test2的值確會消失，有試過在Form2仿照Form1的方式，但這行不通…請問該怎麼才能讓test1、test2的值不管表單切換，還是可以永遠存在，直到離開整個程式！請教指，謝謝！

2007年10月2日 上午 10:30

### 解答

• 程式碼區塊

public partial class Form1 : Form
{
public string t1, t2, t3;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e) //連到Form2
{
t1 = "11111";
t2 = "22222";
t3 = "33333";

Form2 form2 = new Form2();
form2.form1 = this;
form2.ShowDialog();
Close();
}
}

public partial class Form2 : Form
{
internal Form1 form1 = new Form1();
private string tt1, tt2, test1, test2;

public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
tt1 = form1.t1;
tt2 = form1.t2;
textBox1.Text = tt1 + tt2;
}

private void button1_Click(object sender, EventArgs e) //連到Form3
{
test1 = "AAAA";
test2 = "BBBB";
Form3 form3 = new Form3();
form3.form1 = form1; //form1 to form3
form3.form2 = this;
form3.ShowDialog();

textBox2.Text = test1;
textBox3.Text = test2;

}
}

public partial class Form3 : Form
{
internal Form1 form1 = new Form1();
internal Form2 form2 = new Form2();
private string ttt3;

public Form3()
{
InitializeComponent();
}

private void Form3_Load(object sender, EventArgs e)
{
ttt3 = form1.t3;
textBox1.Text = ttt3;
}

private void button1_Click(object sender, EventArgs e) //連到Form2
{
form2.form1 = form1;
form2.Show();
Close();
}
}

PS. Form3 中 new 了一個 Form2 當然 test1 與 test2 會不見阿...

2007年10月2日 上午 11:57

### 所有回覆

• 程式碼區塊

public partial class Form1 : Form
{
public string t1, t2, t3;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e) //連到Form2
{
t1 = "11111";
t2 = "22222";
t3 = "33333";

Form2 form2 = new Form2();
form2.form1 = this;
form2.ShowDialog();
Close();
}
}

public partial class Form2 : Form
{
internal Form1 form1 = new Form1();
private string tt1, tt2, test1, test2;

public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
tt1 = form1.t1;
tt2 = form1.t2;
textBox1.Text = tt1 + tt2;
}

private void button1_Click(object sender, EventArgs e) //連到Form3
{
test1 = "AAAA";
test2 = "BBBB";
Form3 form3 = new Form3();
form3.form1 = form1; //form1 to form3
form3.form2 = this;
form3.ShowDialog();

textBox2.Text = test1;
textBox3.Text = test2;

}
}

public partial class Form3 : Form
{
internal Form1 form1 = new Form1();
internal Form2 form2 = new Form2();
private string ttt3;

public Form3()
{
InitializeComponent();
}

private void Form3_Load(object sender, EventArgs e)
{
ttt3 = form1.t3;
textBox1.Text = ttt3;
}

private void button1_Click(object sender, EventArgs e) //連到Form2
{
form2.form1 = form1;
form2.Show();
Close();
}
}

PS. Form3 中 new 了一個 Form2 當然 test1 與 test2 會不見阿...

2007年10月2日 上午 11:57
• 謝謝您的幫助!問題已解決!

2007年10月4日 上午 07:06