gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:38:22 26 4
gpt4 key购买 nike

请指导我如何在 Visual Studio C# 中取消选中子窗体的父窗体复选框?

这就像按下/单击子表单中的按钮时,应该取消选中父表单中的复选框。

我也在使用 WindowsForm。

请指导

问候

阿萨德

最佳答案

所以,我看到一些...不太好的答案告诉您继续并公开您的复选框。不。从 MDI 窗体中公开一个事件,主窗体在创建时可以处理该事件。当子窗体触发事件时,主窗体可以根据需要更新 UI。

暴露您的 UI 元素确实不是一个好主意。如果您删除该 CheckBox 或想使用不同的控件代替它怎么办?现在您还必须去修改您的子表单,因为他们希望 CheckBox 在那里。您正在泄露实现细节,这只会让事情变得更加复杂。

How to do this? Please guide

我很乐意,尤其是当我认为您选择的答案“正确”时,这实在是太糟糕了。下面是一个简单的代码示例,它定义了一个公开事件的 Form 类。当该事件由父表单处理时,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;
}
}

关于c# - 如何取消选中 C# 中子窗体的父窗体的复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4150958/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com