[window form]在子視窗中關閉子視窗並更新母視窗
很多範例都實作在web form上,目前遇到要在window form上使用
記錄一下過程
1. 拉兩個視窗,這裡Form1為母視窗,Form2為子視窗。
2. 傳送參數定義如下
using System.Collections.Generic;
using System.Text;
namespace FormSubmit.Model
{
public class FormParameter
{
//測試參數
private string _FuncNum;
public string FuncNum
{
set { _FuncNum = value; }
get { return _FuncNum; }
}
}
}
3. 將子視窗的FormClosed事件改寫委派
{
Form2 sub = (Form2)sender;
parm1 = sub.parm2;
this.textBox1.Text = parm1.FuncNum;
}
母視窗程式碼
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace FormSubmit
{
public partial class Form1 : Form
{
public FormSubmit.Model.FormParameter parm1 = new Model.FormParameter();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 a = new Form2();
a.Show();
//委託
a.FormClosed += new FormClosedEventHandler(Form2_FormClosed);
}
void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Form2 sub = (Form2)sender;
parm1 = sub.parm2;
this.textBox1.Text = parm1.FuncNum;
}
}
}
子視窗程式碼
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace FormSubmit
{
public partial class Form2 : Form
{
public FormSubmit.Model.FormParameter parm2 = new Model.FormParameter();
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//傳值
parm2.FuncNum = textBox1.Text;
//觸發Form2_FormClosed
this.Close();
}
}
}
輸出結果
子視窗
母視窗
參考資料