子控件中如何调用父窗体的方法啊
子控件中怎么调用父窗体的方法啊?
父窗体form1中有
public void save()
{
....
}
自定义子控件 myPanel 中有
public void GoToNext()
{
}
现在我想在GoToNext中调用父窗体的save()方法,该怎么写代码啊?
------解决方案--------------------
第一种,在主窗体类中定义一个静态成员,来保存当前主窗体对象,例如: public static yourMainWindow pCurrentWin = null; 然后在主窗体构造函数中,给静态成员初始化,如下: pCurrentWin = this;
那么在子窗体中调用父窗体,能够通过“主窗体类名. pCurrentWin”来操作当前的主窗体。
第二种,是在子窗体中定义一个私有成员,来保存当前主窗体对象,例如: private yourMainWindow pParentWin = null; 然后在子窗体构造函数中,加一参数,如下: public yourChildWindow( yourMainWindow WinMain ) {
pParentWin = WinMain; //Other code }
在主窗体创建子窗体的时候,要把this作为参数来构造子窗体,这样在子窗体中调用父窗体,能够直接用“this.pParentWin”就能够了
但是以上所作的,只是让您能够访问当前主窗体对象,那么如何操作控件,很多人直接修改控件的成员访问符,即把“private”改为“public”,我觉得这样破坏了本身类的封装,所以我比较喜欢的做法是增加公有属性或方法来供调用,例如:
public string ButtonText {
get{ return btn.Text;} set{ btn.Text = value;} }
public void Button_Click() {
this.btnDConvert.PerformClick();//Execute button click }
注意:yourMainWindow 指拟建的主窗口的name(如下例中的form1) yourChildWindow 指拟建的子窗口的name(如下例中的form2) 例子:
比如你在Form1弹出Form2窗体 要想在Form2里访问Form1的方法 或者是属性 是就可以用这个构造方法来实现 //子窗体的代码 private Form1 _f1;
public Form2( Form1 f1 ):this()//这句的意思是 调用本类的构造方法 如果没有这句的话 这个窗体的控件就会显示不出来 {
this._f1=f1; }
//父窗体代码,在你调出Form2的按钮下加上这个
Form2 f2=new Form2(this);//这个的意思是 调用Form2的构造方法,把自身 窗体传过去
f2.show(); //这样 Form1就传到Form2去了 在Form2里就可以调用 Form1的方法
和公开的属性了
------解决方案--------------------
用委托,,,,,
------解决方案--------------------
如果你的控件继承自UserControl,那么会有一个ParentForm属性,引用的就是父窗体
this.ParentForm.Save()
------解决方案--------------------
通过子窗体刷新父窗体的方法
//增加子窗体
private void AddandDelete_Click(object sender,EventArgs e)
{
if(flag == false)//判断标识的值决定是否创建窗体
{
CreateFrmChild();//创建子窗体
}
for(int i = 0; i < this.dataGridView1.Controls.Count; i++)//循环遍历DataGridView控件上的控件集
{
if(this.dataGridView1.Controls[i].Name.Equals(BabyWindow.Name))//当存在子窗体时
{
flag = true;//改变标识Flag的值
break;//退出循环体
}
}
}
#endregion
private void ExitProject_Click(object sender,EventArgs e)
{
Application.Exit();//退出本程序
}//CodeGo.net/
#region 创建子窗体的CreateFrmChild方法
public void CreateFrmChild()
{
Frm_Child BabyWindow = new Frm_Child();//实例化一个子窗体
BabyWindow.MdiParent = this;//设置子窗体的父窗体为当前窗体
this.dataGridView1.Controls.Add(BabyWindow);//在DataGridView控件中添加子窗体
BabyWindow.UpdateDataGridView += new EventHandler(BabyWindow_UpdateDataGridView);
BabyWindow.Show();//显示子窗体
}
//刷新父窗体显示信息
void BabyWindow_UpdateDataGridView(object sender,EventArgs e)
{
if(Frm_Child.GlobalFlag == false) //当单击删除按钮时
{
if(ConnPubs.State == ConnectionState.Closed) //当数据库处于断开状态时
{
ConnPubs.Open(); //打开数据库的连接
}
父窗体form1中有
public void save()
{
....
}
自定义子控件 myPanel 中有
public void GoToNext()
{
}
现在我想在GoToNext中调用父窗体的save()方法,该怎么写代码啊?
------解决方案--------------------
第一种,在主窗体类中定义一个静态成员,来保存当前主窗体对象,例如: public static yourMainWindow pCurrentWin = null; 然后在主窗体构造函数中,给静态成员初始化,如下: pCurrentWin = this;
那么在子窗体中调用父窗体,能够通过“主窗体类名. pCurrentWin”来操作当前的主窗体。
第二种,是在子窗体中定义一个私有成员,来保存当前主窗体对象,例如: private yourMainWindow pParentWin = null; 然后在子窗体构造函数中,加一参数,如下: public yourChildWindow( yourMainWindow WinMain ) {
pParentWin = WinMain; //Other code }
在主窗体创建子窗体的时候,要把this作为参数来构造子窗体,这样在子窗体中调用父窗体,能够直接用“this.pParentWin”就能够了
但是以上所作的,只是让您能够访问当前主窗体对象,那么如何操作控件,很多人直接修改控件的成员访问符,即把“private”改为“public”,我觉得这样破坏了本身类的封装,所以我比较喜欢的做法是增加公有属性或方法来供调用,例如:
public string ButtonText {
get{ return btn.Text;} set{ btn.Text = value;} }
public void Button_Click() {
this.btnDConvert.PerformClick();//Execute button click }
注意:yourMainWindow 指拟建的主窗口的name(如下例中的form1) yourChildWindow 指拟建的子窗口的name(如下例中的form2) 例子:
比如你在Form1弹出Form2窗体 要想在Form2里访问Form1的方法 或者是属性 是就可以用这个构造方法来实现 //子窗体的代码 private Form1 _f1;
public Form2( Form1 f1 ):this()//这句的意思是 调用本类的构造方法 如果没有这句的话 这个窗体的控件就会显示不出来 {
this._f1=f1; }
//父窗体代码,在你调出Form2的按钮下加上这个
Form2 f2=new Form2(this);//这个的意思是 调用Form2的构造方法,把自身 窗体传过去
f2.show(); //这样 Form1就传到Form2去了 在Form2里就可以调用 Form1的方法
和公开的属性了
------解决方案--------------------
用委托,,,,,
------解决方案--------------------
如果你的控件继承自UserControl,那么会有一个ParentForm属性,引用的就是父窗体
this.ParentForm.Save()
------解决方案--------------------
通过子窗体刷新父窗体的方法
//增加子窗体
private void AddandDelete_Click(object sender,EventArgs e)
{
if(flag == false)//判断标识的值决定是否创建窗体
{
CreateFrmChild();//创建子窗体
}
for(int i = 0; i < this.dataGridView1.Controls.Count; i++)//循环遍历DataGridView控件上的控件集
{
if(this.dataGridView1.Controls[i].Name.Equals(BabyWindow.Name))//当存在子窗体时
{
flag = true;//改变标识Flag的值
break;//退出循环体
}
}
}
#endregion
private void ExitProject_Click(object sender,EventArgs e)
{
Application.Exit();//退出本程序
}//CodeGo.net/
#region 创建子窗体的CreateFrmChild方法
public void CreateFrmChild()
{
Frm_Child BabyWindow = new Frm_Child();//实例化一个子窗体
BabyWindow.MdiParent = this;//设置子窗体的父窗体为当前窗体
this.dataGridView1.Controls.Add(BabyWindow);//在DataGridView控件中添加子窗体
BabyWindow.UpdateDataGridView += new EventHandler(BabyWindow_UpdateDataGridView);
BabyWindow.Show();//显示子窗体
}
//刷新父窗体显示信息
void BabyWindow_UpdateDataGridView(object sender,EventArgs e)
{
if(Frm_Child.GlobalFlag == false) //当单击删除按钮时
{
if(ConnPubs.State == ConnectionState.Closed) //当数据库处于断开状态时
{
ConnPubs.Open(); //打开数据库的连接
}