目的:
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总是为空请各位高手给予分析,谢谢~~~
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总是为空请各位高手给予分析,谢谢~~~
解决方案 »
- C#监听问题
- listbox不能同步更新的问题
- telnet协议解析中文问题
- 添加了using System.Data.SqlClient、using System.Data、using System.Configuration,还是提示SqlConnection找不到命空间……
- 关于使用WebBrowser控件,实现MyIE的ViewPage功能?
- WindowsPhone7 不识别 using WindowsPhoneNavigation.Misc;
- 正则表达式:可以由a-z的小写英文字母、0-9的数字、下划线组成,长度在4-16个字符,不能单独使用数字
- 如何将输入的角度转化为弧度?
- 请帮忙看看,为什么不触发事件??????????????????、
- 一个很弱的问题关于数组的
- 很简单的ado.net连接sql数据库问题数据库,请高手帮下
- C#做三年从未遇到的奇怪问题,关于数据集的!在线等
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;
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();
}
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在构造中是无法获得的,楼主要讲出自己的目的,我才好给你进一步代码修正
form1 _OwnerForm;
public form2(form1 OwnerForm)
{
_OwnerForm=OwnerForm;
InitializeComponent();_OwnerForm.frmLostFocus += new ActionEventHandle(Form1_frmLostFocus);
}