如果我明白你的問題:
- 窗體2有2個文本框(textBox1
和textBox2
)和2個按鈕(btnOK
和btnCancel
)
- 如果按下btnOK
- 協調值textBox1
和textBox2
並將它們傳遞給Form1
- 如果btnCancel
按下 - 不傳遞任何數據
我的回答簡述:
它可以與事件處理程序可以輕鬆實現,只是掛鉤到OnFormClosing
事件,並從Form2
預定義屬性讀取數據一些代碼來說明我的回答是低於
Form1.cs的
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
frm2.FormClosing += new FormClosingEventHandler(frm2_FormClosing);
}
void frm2_FormClosing(object sender, FormClosingEventArgs e)
{
if ((sender as Form2).textData != null)
textBox1.Text = (sender as Form2).textData;
}
}
Form2.cs
public partial class Form2 : Form
{
public string textData;
public Form2()
{
InitializeComponent();
}
private void btnOK_Click(object sender, EventArgs e)
{
textData = textBox1.Text + " " + textBox2.Text;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
然而類似於例如另一個問題http://stackoverflow.com/questions/1806795/how-to-change-text-in-a-textbox-on-another-form-in-visual-c?rq=1 – 2013-02-22 15:57:24