日期:2014-05-19  浏览次数:20854 次

如何弹出的模态窗体调用父窗体的事件
在Form1中点击Button1弹出Form2(模态的),如何在Form2中点击Button的时候调用Form1中的一个事件(用于刷新Form1窗体的信息),这样在Form2总进行操作的时候,就可以在Form1中看到操作的结果.

------解决方案--------------------
在Form2類里面添加一個Form1的變量,在點Form2里的Button時,執行Form1的事件函數
class Form2:...
{
public Form2(Form f)
{
parForm=f;
}

private Form1 parForm;
....
private void ButtonClick(..//Button事件函數
{
parForm.方法()
}
}
------解决方案--------------------
你可以在Form2上添加一个事件,然后在Form1中对这个事件处理就可以了.
大致如下:

1:Form2中
public event EventHandler OnEvent;
在按钮事件里
if (this.OnEvent!=null)
{
this.OnEvent(this, new EventArgs());
}

2:在Form1中:

Form2 f=new Form2();
f.OnEvent+=new EventHandler(this.f_OnEvent);
f.ShowDialog(this);

private void f_OnEvent(object sender, EventArgs e)
{
this.操作.
}
------解决方案--------------------
jiazheng(飞天) 正解.

比如 根据 Form2(弹出的模态窗体) 中 textBox1.Text 值改变更新 Form1 中 textBox1.text的值:
一: 在Form1 中:
写一个属性:
public string TextBoxValue
{
set { this.textBox1.Text=value; }
}


在Form2 中:
private Form1 frm1;
public Form1(Form1 f1)
{
this.frm1=f1;
}
private textBox1_TextChange(.......)
{
frm1.TextBoxValue=this.textBox1.Text;
}