简体   繁体   English

如何从C#的子窗体中取消选中父窗体的复选框?

[英]How to uncheck a checkbox of a parent form from a child form in C#?

Please guide me as how to uncheck a checkbox of a parent form from a child form in Visual studio C#? 请指导我如何在Visual Studio C#中从子窗体中取消选中父窗体的复选框?

Its like when a button in child form is pressed/clicked, then a checkbox in parent form should be unchecked. 就像按下/单击子窗体中的按钮时,应取消选中父窗体中的复选框。

Also I am using WindowsForm. 我也在使用WindowsForm。

Please guide 请指导

Regards 问候

Asad 阿萨德

So, I see some... not so good answers telling you to go ahead and make your checkbox public. 所以,我看到了一些……不是很好的答案,告诉您继续进行操作,并公开您的复选框。 Don't. 别。 Expose an event from your MDI forms that the main form can handle as they are created. 从您的MDI表单中公开一个事件,该事件在主表单创建时即可处理。 When the child form fires the event the main form can update the UI as it sees fit. 当子窗体触发事件时,主窗体可以根据需要更新UI。

It's really not a good idea to expose your UI elements. 公开您的UI元素确实不是一个好主意。 What if you remove that CheckBox or want to use a different control in its stead? 如果删除该CheckBox或想使用其他控件代替该怎么办? Now you have to go and modify your child forms as well because they expect that CheckBox to be there. 现在,您还必须去修改子窗体,因为他们希望CheckBox在那里。 You are leaking implementation details and that really just makes things more complicated down the road. 您正在泄漏实现细节,这实际上只会使事情变得更加复杂。

How to do this? 这个怎么做? Please guide 请指导

I would be happy to, especially when I consider the answer you chose as "correct", which is honestly plain awful. 我很乐意,尤其是当我认为您选择的答案是“正确”时,这实在是太糟糕了。 Here is a simple code sample that defines a Form class which exposes an event. 这是一个简单的代码示例,该示例定义了一个暴露事件的Form类。 When that event is handled by the parent form, the UI is updated. 当该事件由父表单处理时,UI将更新。

public class ChildForm
{
    public event EventHandler SomeEvent;

    protected virtual void OnSomeEvent( EventArgs e )
    {
        EventHandler del = SomeEvent;
        if( del != null )
        {
            // fire the event
            del( this, e );
        }
    }

    private void someButton_Click( object sender, EventArgs e )
    {
        // for the sake of example, let's assume that you want
        // to notify listeners of "SomeEvent" when a button is clicked
        OnSomeEvent( this, EventArgs.Empty );
    }
}

public class MainForm : Form
{
    private void ShowChildForm( )
    {
        using( ChildForm frm = new ChildForm() )
        {
            frm.SomeEvent += frm_SomeEvent;
            frm.ShowDialog();
        }
    }

    private void frm_SomeEvent( object sender, EventArgs e )
    {
        // this is where we handle the event "SomeEvent" that the 
        // child form fires when you need to communicate that something has happened.
        // now you may update the UI as needed and the ChildForm class does not have
        // to know anything about how the UI is actually implemented.
        // this is referred to as "loose coupling" and makes your code more maintainable.
        someCheckbox.Checked = true;
    }
}

It would be best practice not to have a direct reference/dependency from your child form to your parent form. 最好的做法是不要直接从您的孩子表单到您的父母表单之间进行引用/依赖。 Your child form should not have the responsibility to uncheck the checkbox on your parent form or even know of the existence of your parent form (se the Single responsibility principle ). 您的子窗体不应该负责取消选中父窗体上的复选框,甚至不知道您的父窗体是否存在(即“ 单一责任原则” )。

One way to obtain this is to use the Observer pattern by using an event on the child form which the parent form can listen to. 一种获取方法是通过在子窗体上使用父窗体可以监听的事件来使用观察者模式 Here is a event tutorial: http://msdn.microsoft.com/en-us/library/aa645739(VS.71).aspx . 这是一个事件教程: http : //msdn.microsoft.com/zh-cn/library/aa645739(VS.71).aspx

A code example for doing this (only partial code shown): 一个执行此操作的代码示例(仅显示部分代码):

//Parent form.    
public partial class Form1 : Form
{
    //This is the checkbox on your parent form
    CheckBox myCheckBox = new CheckBox();

    public Form1()
    {
        InitializeComponent();

        Form2 form2 = new Form2();
        form2.MyUncheckingEvent += new EventHandler(form2_MyUncheckingEvent);
    }

    void form2_MyUncheckingEvent(object sender, EventArgs e)
    {
        myCheckBox.Checked = false;
    }
}

//Child form    
public partial class Form2 : Form
{
    public event EventHandler MyUncheckingEvent;

    public Form2()
    {
        InitializeComponent();
    }

    public void MethodFormRaisingTheEvent()
    {
        if (MyUncheckingEvent != null)
        {
            MyUncheckingEvent(this, EventArgs.Empty);
        }
    }
}

Cast the Parent property into the main form's class type. Parent属性转换为主窗体的类类型。
For example, in the context of the child form: 例如,在子窗体的上下文中:

(this.Parent as frmMain).MyCheckBox.Checked = false;   

see my question get-back-hidden-form-from-another-form 看到我的问题从另一种形式找回隐藏的形式

in addition to that you have to implement like this: 除此以外,您还必须像这样实现:

//Form2
private Form refToForm1;
public Form RefToForm1
{
   get { return refToForm1; }
   set { refToForm1 = value; }
}


 // On buttonClick
       CheckBox cb=(CheckBox)this.RefToForm1.Controls["checkBox1"];
       cb.Checked = !cb.Checked;


//Form1

 Form2 obj2 = new Form2();
 obj2.RefToForm1 = this;
 obj2.Show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM