C# Winforms - 将生存状态从一个用户控件切换到另一个用户控件? [英] C# Winforms - Toggle viability states from one usercontrol to another usercontrol?

查看:40
本文介绍了C# Winforms - 将生存状态从一个用户控件切换到另一个用户控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我附上了一个例子,说明我想用下面的代码完成什么.如果可能的话,有人可以写一个片段或解释它是如何工作的吗?我尝试了几种不同的方法,但我的尝试都没有成功(我不断收到范围错误或无法识别用户控件).

I have attached an example of what I am trying to accomplish with my code below. If possible could someone write a snippet or explain how this could work? I have tried several different ways and none of my attempts have panned out (I keep getting scope errors or that the usercontrol is unrecognized).

目前正在尝试让 UserControl1.Button1 切换 UserControl2 上的可见性状态.

Currently trying to get UserControl1.Button1 to toggle the visibility state on UserControl2.

层次结构:

  1. Form1
  1. 用户控件1
  1. 按钮 1

  • 用户控件2

  • UserControl2

    1. 按钮 1

  • 我可以得到如下所示的变体来工作,但是 usercontrol 到 usercontrol 似乎需要一些额外的步骤.帮助!

    I can get variations of this to work shown below, but usercontrol to usercontrol seems to require some extra steps. Help!

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    
        private void HIDE2_Click(object sender, EventArgs e)
        {
            userControl21.Hide();
        }
    

    推荐答案

    正如@hans-passant 指出的那样.

    As @hans-passant pointed out.

    事件的使用为我们提供了一种使用观察者模式的简单方法.

    The use of events gives us an easy way to employ the observer pattern.

    UserControl1 中实现自定义 Event 并在单击隐藏第二个控件的按钮时调用此事件(通知观察者有关更改):

    Implement a custom Event In UserControl1 and invoke this event (notify the observers about a change) when the button to hide the second control is clicked:

    public partial class UserControl1 : UserControl
    {
        public event EventHandler HideRequested;
    
        ...
    
        private void button1_Click(object sender, EventArgs e)
        {
            if (HideRequested != null)
            {
                HideRequested.Invoke(this, EventArgs.Empty);
            }
        }
    
    }
    

    在 Form1 中,通过实现事件处理程序附加到事件:

    The in Form1, attach to the event by implementing an event handler:

        public Form1()
        {
            InitializeComponent();
            userControl11.HideRequested += userControl11_HideRequested;
        }
    
        void userControl11_HideRequested(object sender, EventArgs e)
        {
            userControl21.Hide();
        }
    

    当然还有其他方法可以实现这一点,但任何其他方法都可能会在您的代码中引入耦合.例如,这也是有效的(通过将用户控件 2 的访问修饰符更改为 public):

    There are of course other ways to achieve this, but any other way will probably introduce coupling in your code. For example, this is also valid (by changing the access modifier of user control 2 to public):

        private void button1_Click(object sender, EventArgs e)
        {
            (Parent as Form1).userControl21.Hide();
        }
    

    你也可以这样想:(那你就不用把USerControl2的访问修饰符改成public了)

    You can also think of something like this: (then you don't have to change the access modifier of USerControl2 to public)

    在 Form1 中:

        public void HideUserControl2()
        {
            userControl21.Hide();
        }
    

    在 UserControl1 button_click 中:

    And in the UserControl1 button_click:

        private void button1_Click(object sender, EventArgs e)
        {
            (Parent as Form1).HideUserControl2();
        }
    

    这篇关于C# Winforms - 将生存状态从一个用户控件切换到另一个用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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