目的:
    Form1中的右键菜单点击修改过后,需要将Form1中列表的id值和其它一些属性传到Form2方法:采用自定义事件的方法疑问:
    我是一新手,对事件机制、变量生存周期不是很清楚,请各位帮忙分析问题出在哪里,谢谢~~~~偶的代码:
1、定义一委托
    public delegate void ActionEventHandle (FrmChangeArgs e);
2、定义的事件参数
public class FrmChangeArgs:System.EventArgs
{
public FrmChangeArgs(int index)
{
_index=index;
} private int _index;
public int Index
{
get
{
return _index;
}
set
{
_index=value;
}
}
3、Form1中的代码
 //定义事件   
 public event ActionEventHandle frmLostFocus;
 protected virtual void OnfrmLostFocus(FrmChangeArgs e)
 {
   if (frmLostFocus!=null)
frmLostFocus(e);
  }
//在右键菜单修改处调用
private void menuItem2_Click(object sender, EventArgs e)
{
string index=this.dataGrid1[this.dataGrid1.CurrentRowIndex,0].ToString();
FrmChangeArgs arg=new FrmChangeArgs(Convert.ToInt16(index));
OnfrmLostFocus(arg);
Form2 frmChild=new Form2 ();
frmChild.Owner = this;
frmChild.ShowDialog();
...
}
4、Form2的代码
private void Form2_Load(object sender, EventArgs e)
{
//注册事件
Form1 parentFrm=(Form1)this.Owner;
parentFrm.frmLostFocus += new ActionEventHandle(Form1_frmLostFocus);
if (index!=-1) //其中index是该页面私有成员,初始值为-1
{
  //说明是修改页面进入
  InitData();
}
...
}
private void Form1_frmLostFocus( FrmChangeArgs e)
{
index=e.Index ;
}我在跟踪时候发现:
  A)在form2中,我的index始终是-1
  B)在FORM1中,我发现进入OnfrmLostFocus,frmLostFocus总是为空请各位高手给予分析,谢谢~~~

解决方案 »

  1.   

    OnfrmLostFocus(arg);
    Form2 frmChild=new Form2 ();
    frmChild.Owner = this;
    frmChild.ShowDialog();这里的顺序搞错了,下面这样应该可以的:Form2 frmChild=new Form2 ();
    frmChild.Owner = this;
    frmChild.ShowDialog();
    OnfrmLostFocus(arg);
      

  2.   

    OnfrmLostFocus(arg);
    Form2 frmChild=new Form2 ();
    frmChild.Owner = this;
    frmChild.ShowDialog();这里的顺序搞错了,下面这样应该可以的:Form2 frmChild=new Form2 ();
    frmChild.Owner = this;
    frmChild.ShowDialog();
    OnfrmLostFocus(arg);--------------------------------------
    这里也错了吧`~应该在打开窗体前就已经把参数传递过去了`
    Form2 frmChild=new Form2 ();
    frmChild.Owner = this;
    OnfrmLostFocus(arg);
    frmChild.ShowDialog();
    ------------------------------------还有 为何不用直接form2的构造函数接收参数???
    Form2 frmChild=new Form2 ();
    frmChild.Owner = this;
    frmChild.ShowDialog(arg);
    form2的构造函数
    public Flag(DataTable dt)
            {
                _dt = dt;
                InitializeComponent();
            }
      

  3.   

    Form2 frmChild=new Form2 ();
    frmChild.Owner = this;
    OnfrmLostFocus(arg);
    frmChild.ShowDialog();也错,他是在load里面加载事件的,你不showdialog,怎么会到load呢你这里主要问题是注册事件的时间与new form1的时间对不上,事件已经发生了,但里面还没注册。我先分析楼主
    FrmChangeArgs arg=new FrmChangeArgs(Convert.ToInt16(index));//新建事件
    OnfrmLostFocus(arg);//发生事件,但if (frmLostFocus!=null)
    LostFocus(e);这里的确是等于null,因为你没注册
    Form2 frmChild=new Form2 ();//新建窗口
    frmChild.Owner = this;
    frmChild.ShowDialog();//LOAD函数,这时你才注册事件Form2 frmChild=new Form2 ();//新建窗口
    frmChild.Owner = this;
    frmChild.ShowDialog();//LOAD函数,这时你才注册事件
    OnfrmLostFocus(arg);//发生事件,看似对了,但showdialog必须发生窗体关闭才会到下一行,也就是窗体不关闭的时候,不会运行到此行。Form2 frmChild=new Form2 ();//新建窗口
    frmChild.Owner = this;
    OnfrmLostFocus(arg);//发生事件,但if (frmLostFocus!=null) LostFocus(e);这里的确是等于null,因为你没注册
    frmChild.ShowDialog();//注册事件,一样是不行本来注册时间应该放在form2的构造函数就可以了,但owner在构造中是无法获得的,楼主要讲出自己的目的,我才好给你进一步代码修正
      

  4.   

    或者不用owner方式获得form1,直接用构造函数参数传入form1吧,但你的设计目的我还是不太了解
    form1 _OwnerForm;
    public form2(form1 OwnerForm)
    {
    _OwnerForm=OwnerForm;
    InitializeComponent();_OwnerForm.frmLostFocus += new ActionEventHandle(Form1_frmLostFocus);
    }
      

  5.   

    感谢各位的分析,其实对于利用构造函数、属性传值我都知道,只是在尝试利用自定义事件来进行传值~~~(^_^,不然我的窗体名称不能为FORM1/FORM2吧)感谢上边各位的分析,尤其是zhuqueta0101
      

  6.   

    ^_^,很有意思的问题~~~~楼主,是不是把WebForm和WinForm两种相应机制搞混淆了?WinForm中,对于代码顺序,这个是很重要的,而WebForm涉及到Server与Client相应,所以在一个事件完全执行完毕过后,而后再Page_Load,想必我不说这些,楼主也应该知道zhuqueta0101() 分析是正确的,我想你期望达到的目的这个老兄已经帮你达到了