如何启用C#.net中子窗体中父窗体中的按钮 [英] how to enable the buttons which are in parent form from child form in C#.net

查看:389
本文介绍了如何启用C#.net中子窗体中父窗体中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
在父窗体中,我将button属性设置为enable false,以及如何从C#.net的子窗体(Windows窗体)中启用按钮?

Hi all
In parent form I put the button property as enable false and how to enable the buttons from child form in C#.net (windows forms)?

推荐答案

为父表单转换为子表单,将按钮变量设为公开,并设置parent.button.Enabled = false;
Pass a variable for parent form to child form, make the button variable public and set parent.button.Enabled = false;


Hi,

您可以按照以下方式进行操作

1.显示子窗口,并在显示子窗口时,将子表单对象的owner属性设置为父表单,如下所示

Hi,

You can do that in following way

1. show the child window and while showing child, set the owner property of child form object to the parent form as shown following

private void btnShowChild_Click(object sender, EventArgs e)
{
            ChildForm ch = new ChildForm();
            ch.Owner = this;
            ch.ShowDialog();
}



2.在childForm中,获取对Parent表单的引用(先前已将其设置为childForm的所有者),如下所示:



2. In childForm, get the reference to Parent form (which was earlier set as owner of the childForm ) as shown here

ParentForm p = (ParentForm)this.Owner;



3.一旦从子表单中引用了父表单对象实例,就可以使用以下方式获得任何控件(例如parentForm中名为"button2"的按钮)



3. Once you have the reference to parent form object instance from child form, you get any control(say a button named "button2" in parentForm) using following way

private void btnDisableParentButton_Click(object sender, EventArgs e)
{

            ParentForm p = (ParentForm)this.Owner;
            Control[] c = p.Controls.Find("button2", true);
            Button b = (Button)c[0];
            b.Enabled = false;
}



注意:我正在从子窗口中禁用父窗体中名为"button2"的按钮.

请让我知道这对您是否有用.

谢谢
Arindam D Tewary



NB: I am disabling a button named "button2" in Parent form from a child window.

Please let me know if that was usefull to you.

Thanks
Arindam D Tewary


这篇关于如何启用C#.net中子窗体中父窗体中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆