你这还是双向的呢//主窗体注册时 SubFormEvent += new EventHandler<SubFormEventArgs>(SubFormEvent); void SubFormEvent(object sender, SubFormEventArgs e) { //if("完成了" && e.Action != null) //e.Action(); } // public event EventHandler<SubFormEventArgs> SubFormEvent; public class SubFormEventArgs : EventArgs { public Action Action { get; set; } public SubFormEventArgs(Action completedAction) { this.Action = completedAction; } } 就是往你自定义事件里加个Action 参数
父窗口 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace 测试项目 { public partial class Form1 : Form { public Form1() { InitializeComponent(); //System.Net.WebRequestMethods.Ftp.DownloadFile } private void button1_Click( object sender, EventArgs e ) { Form2 frm2 = new Form2(); frm2.OnXXXXing += new Form2.InfoHandler( frm2_OnXXXXing ); frm2.OnXXXXed += new Form2.InfoHandler( frm2_OnXXXXed ); frm2.Show(); } void frm2_OnXXXXed() { MessageBox.Show( "处理后" ); } void frm2_OnXXXXing() { MessageBox.Show( "处理前" ); } } } 子窗口 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace 测试项目 { public partial class Form2 : Form { public event InfoHandler OnXXXXing; public event InfoHandler OnXXXXed; public delegate void InfoHandler(); void Info() { } public Form2() { InitializeComponent(); } private void button1_Click( object sender, EventArgs e ) { if( OnXXXXing != null ) { OnXXXXing(); } //特有逻辑 System.Threading.Thread.Sleep( 1000 * 3 ); if( OnXXXXed != null ) { OnXXXXed(); } } } }
//子窗口中: public delegate void PictureClick(int iIndex, string strConName); class ChildFrom { public event PictureClick ButtonClick; //命名事件
public void Test() { //传出事件 ButtonClick( 1, "Test" ); } }//父窗口中: class ParentFrom { private ChildFrom; public void SetEvent() { ChildFrom = new ChildFrom(); ChildFrom.ButtonClick += this.PictureClick; } private void PictureClick(int iIndex, string strConName) { Console.WriteLine(strConName); } }//当子窗体的Test方法被执行时, 就回传事件至父窗体
if(str == false)
{
//没完成
}
else
{
//完成
}
则子窗口注册父窗口事件后,事件被主窗口触发后可直接在子窗口里处理,处理完即处理完,无须再次通知子窗口,因为事件处理代码本身即位于子窗口中...如果是通过子窗口事件
则设计两个子窗口事件,分别为onXXXXing和onXXXXed事件,分别表示在某事件开始前和开始后
让父窗口注册此两个事件,在父窗口里处理事件,
这样在子窗口的onXXXXed事件里表明事件已处理完毕(有点拗口)
SubFormEvent += new EventHandler<SubFormEventArgs>(SubFormEvent); void SubFormEvent(object sender, SubFormEventArgs e)
{
//if("完成了" && e.Action != null)
//e.Action();
} //
public event EventHandler<SubFormEventArgs> SubFormEvent; public class SubFormEventArgs : EventArgs
{
public Action Action
{
get;
set;
} public SubFormEventArgs(Action completedAction)
{
this.Action = completedAction;
}
}
就是往你自定义事件里加个Action 参数
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace 测试项目 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); //System.Net.WebRequestMethods.Ftp.DownloadFile
} private void button1_Click( object sender, EventArgs e ) {
Form2 frm2 = new Form2();
frm2.OnXXXXing += new Form2.InfoHandler( frm2_OnXXXXing );
frm2.OnXXXXed += new Form2.InfoHandler( frm2_OnXXXXed );
frm2.Show();
} void frm2_OnXXXXed() {
MessageBox.Show( "处理后" );
} void frm2_OnXXXXing() {
MessageBox.Show( "处理前" );
}
}
}
子窗口
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace 测试项目 {
public partial class Form2 : Form {
public event InfoHandler OnXXXXing;
public event InfoHandler OnXXXXed;
public delegate void InfoHandler();
void Info() { } public Form2() {
InitializeComponent();
} private void button1_Click( object sender, EventArgs e ) {
if( OnXXXXing != null ) {
OnXXXXing();
}
//特有逻辑
System.Threading.Thread.Sleep( 1000 * 3 ); if( OnXXXXed != null ) {
OnXXXXed();
}
}
}
}
public delegate void PictureClick(int iIndex, string strConName);
class ChildFrom
{
public event PictureClick ButtonClick; //命名事件
public void Test()
{
//传出事件
ButtonClick( 1, "Test" );
}
}//父窗口中:
class ParentFrom
{
private ChildFrom;
public void SetEvent()
{
ChildFrom = new ChildFrom();
ChildFrom.ButtonClick += this.PictureClick;
} private void PictureClick(int iIndex, string strConName)
{
Console.WriteLine(strConName);
}
}//当子窗体的Test方法被执行时, 就回传事件至父窗体