在用户控件单击事件中以主窗体切换用户控件 [英] switch usercontrols in mainform within a usercontrol click event

查看:79
本文介绍了在用户控件单击事件中以主窗体切换用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来可能很愚蠢,但是我很难弄清楚这一点.任何帮助将不胜感激:

This may sound stupid, But I am having hard time to figure this out; any help would be appreciated:

我有两个用户控件,分别为" Safety_Check "和" OEE_Track ".在我的 MainForm 中,有一个名为" pnl_main_controller "的面板,这是我同时显示两个用户控件的地方.我在主窗体上有两个按钮,并且可以在两者之间进行动态切换而没有任何问题.

I have two user controls called "Safety_Check" and "OEE_Track". In my MainForm I have a panel called "pnl_main_controller" this is where I am displaying both my user controls. I have two buttons on my main form and I am dynamically switching between both without any issue.

安全性检查用户控件;

public partial class Safety_Check : UserControl
    {
        private static Safety_Check _instance;

        public static Safety_Check instance
        {
            get
            {
                if (_instance == null)
                    _instance = new Safety_Check();
                return _instance;
            }
        }

        public Safety_Check()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ///////------------------------
        }

    }

OEE_Track用户控件

public partial class OEE_Track : UserControl
    {

        private static OEE_Track _instance;

        public static OEE_Track instance
        {
            get
            {
                if (_instance == null)
                    _instance = new OEE_Track();
                return _instance;
            }
        }

        public OEE_Track()
        {
            InitializeComponent();
        }
    }

MainForm:

public partial class MainForm : Form
    {
     private void btn_reg_Click(object sender, EventArgs e)
        {
            if (!pnl_main_controller.Contains(Safety_Check.instance))
            {
                pnl_main_controller.Controls.Add(Safety_Check.instance);
                Safety_Check.instance.Dock = DockStyle.Fill;
                Safety_Check.instance.BringToFront();
            }
            else
            {
                Safety_Check.instance.BringToFront();
            }        }

       private void btn_OEE_Click(object sender, EventArgs e)
        {
            if (!pnl_main_controller.Contains(OEE_Track.instance))
            {
                pnl_main_controller.Controls.Add(OEE_Track.instance);
                OEE_Track.instance.Dock = DockStyle.Fill;
                OEE_Track.instance.BringToFront();
            }
            else
            {
                OEE_Track.instance.BringToFront();
            }
     }

我想做的是在我的" 安全检查 "上有一个名为" Button1 "的按钮"用户控件,每当我按此按钮时,我都希望" 安全性检查 "消失在" pnl_main_controller "上并带来" OEE_Track "进入面板

What I am trying to do is I have a button called "Button1" on my "Safety_Check" Usercontrol, whenever I press this , I want "Safety_Check" to be disappear on "pnl_main_controller" and bring "OEE_Track" to the panel

推荐答案

对于控件之间的交互,有几种解决方案.控件是类,并且像其他任何类一样,控件可以使用它们的公共属性和方法或使用某些介体相互交互.

There are several solutions for interaction between controls. Controls are classes and like any other class they can interact with each other using their public properties and methods or using some mediator.

在这种情况下,您的控件不需要彼此了解,也不需要直接进行交互:

In this case, your controls don't need to know each other and don't need to interact to each other directly:

  • 他们可以要求另一个同时知道两个控件的对象来完成这项工作.
  • 或者他们可以提出他们的请求通知,订阅该通知的人将为其提供服务.

要让另一个对象为他们完成这项工作,您有多种解决方案.例如,您可以在父窗体和子控件中实现特定的接口,将父类型转换为该特定接口,然后调用特定的方法来为您完成工作.

To ask another object to do the job for them you have multiple solutions. As an example you can implement a specific interface in the parent form and in the child controls, cast the parent to that specific interface and call a specific method which do the job for you.

对于引发请求通知,一种简单的解决方案是依靠事件.您可以在子控件中创建一个事件,并在需要父控件为您做一些事情时引发它.然后在父项中订阅该事件并完成工作.

For raising the request notification, an easy solution is relying on events. You can create an event in the child control and raise it when you need the parent do something for you. Then in the parent subscribe to that event and do the job.

示例-使用事件

我假设您的 UserControl1 内部具有 Button1 ,并且您已经处理了 Button1 Click 事件.然后,您可以创建 Button1Clicked 事件,并在单击 Button1 时引发该事件:

I assume you have UserControl1 having Button1 inside and you have handled Click event of Button1. Then you can create Button1Clicked event and raise it when Button1 clicked:

public event EventHandler Button1Clicked;
private void Button1_Click(object sender, EventArgs e)
{
    Button1Clicked?.Invoke(this, e);
}

然后在父表单中,订阅该事件并执行您想做的所有事情:

Then in the parent form, subscribe for the event and do whatever you want:

private void userControl11_Button1Clicked(object sender, EventArgs e)
{
    //Hide userControl11 and show userControl21 
}

示例-使用界面

我认为,您有一个包含一些标准方法的接口:

I assume, you have an interface having a few standard methods:

public interface IDoSomething
{
    void DoSomething();
    void DoSomethingElse();
}

并且您已经以父级形式实现了该接口:

And you have implemented the interface in your parent form:

public class Form1: Form, IDoSomething
{
    // ...

    public void DoSomething()
    {
        //Hide userControl11 and show userControl21 
    }
    public void DoSomethingElse()
    {
        // ...
    }
}

然后在用户控件中:

private void Button1_Click(object sender, EventArgs e)
{
    var f = FindForm() as IDoSomething;
    if(f!=null)
        f.DoSomething();
}

这篇关于在用户控件单击事件中以主窗体切换用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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