在 .NET 2.0 中隐藏与 UserControl 关联的工具箱的最佳方法?

发布于 2024-07-25 06:34:00 字数 1801 浏览 1 评论 0原文

我最近遇到了以下情况。 我有一个可以显示无模式工具箱的UserControl。 如果用户已显示工具箱,我想随着 UserControl 本身变得不可见或可见而相应地隐藏和显示它。 UserControl 可以嵌入任意数量的父容器中,例如 GroupBoxTabPage,这可以影响 >UserControl 实际上是可见的,尽管它自己的 Visible 属性为 True

在 WPF 中,似乎我可以使用 UserControlIsVisibleChanged 事件来处理此问题。 WinForms 有等效的吗? .NET 2.0 中的通用解决方案是什么?

编辑:这是我得出的解决方案。 有更好的解决方案吗?

public partial class MyControl : UserControl
{
    private List<Control> _ancestors = new List<Control>();
    private bool _isVisible = false;

    public MyControl()
    {
        InitializeComponent();

        ParentChanged += OnParentChanged;
        VisibleChanged += OnVisibleChanged;
    }

    private void OnParentChanged(object sender, EventArgs e)
    {
        foreach (Control c in _ancestors)
        {
            c.ParentChanged -= OnParentChanged;
            c.VisibleChanged -= OnVisibleChanged;
        }

        _ancestors.Clear();

        for (Control ancestor = Parent; ancestor != null; ancestor = ancestor.Parent)
        {
            ancestor.ParentChanged += OnParentChanged;
            ancestor.VisibleChanged += OnVisibleChanged;

            _ancestors.Add(ancestor);
        }
    }

    private void OnVisibleChanged(object sender, EventArgs e)
    {
        bool isVisible = Visible;
        foreach (Control c in _ancestors)
        {
            if (!c.Visible)
            {
                isVisible = false;
                break;
            }
        }

        if (isVisible != _isVisible)
        {
            _isVisible = isVisible;

            // Control visibility has changed here
            // Do something
        }
    }
}

I've recently come across the following situation. I have a UserControl that can show a modeless toolbox. If the user has shown the toolbox, I'd like to hide and show it appropriately as the UserControl itself becomes invisible or visible, respectively. The UserControl can be embedded in an arbitrary number of parent containers, such as a GroupBox or TabPage, which can influence whether or not the UserControl is actually visible despite its own Visible property being True.

In WPF, it seems like I could use the IsVisibleChanged event of the UserControl to handle this. Is there an equivalent for WinForms? What would a general solution in .NET 2.0 be?

EDIT: Here is the solution that I arrived at. Is there a better solution?

public partial class MyControl : UserControl
{
    private List<Control> _ancestors = new List<Control>();
    private bool _isVisible = false;

    public MyControl()
    {
        InitializeComponent();

        ParentChanged += OnParentChanged;
        VisibleChanged += OnVisibleChanged;
    }

    private void OnParentChanged(object sender, EventArgs e)
    {
        foreach (Control c in _ancestors)
        {
            c.ParentChanged -= OnParentChanged;
            c.VisibleChanged -= OnVisibleChanged;
        }

        _ancestors.Clear();

        for (Control ancestor = Parent; ancestor != null; ancestor = ancestor.Parent)
        {
            ancestor.ParentChanged += OnParentChanged;
            ancestor.VisibleChanged += OnVisibleChanged;

            _ancestors.Add(ancestor);
        }
    }

    private void OnVisibleChanged(object sender, EventArgs e)
    {
        bool isVisible = Visible;
        foreach (Control c in _ancestors)
        {
            if (!c.Visible)
            {
                isVisible = false;
                break;
            }
        }

        if (isVisible != _isVisible)
        {
            _isVisible = isVisible;

            // Control visibility has changed here
            // Do something
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聽兲甴掵 2024-08-01 06:34:00

不幸的是,当使用多个表单或控件时,诸如 Visible 之类的属性要求您手动设置它们。 遍历其属性依赖于其他控件的属性的所有控件是一件痛苦的事情,而且有些乏味。

如果您设置 UserControl.Tag 来引用您的工具箱会怎么样?

Unfortunately, when working with multiple forms or controls, properties like Visible require that you set them manually. It's a pain, and somewhat tedious to go through all controls whose properties are dependent on other controls' properties.

What if you set the UserControl.Tag to reference your toolbox?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文