-->

从/通信以在形式的tabcontrol用户控件(Communicating from/to user

2019-10-18 17:36发布

我认为C#是很难。 尝试张贴在计算器的问题。

我有一个列表框和按钮的用户控件,自己在一个TabControl,本身就是一种形式的标签页。 我需要的是单击按钮时从表单填充列表框。

形成>的tabcontrol>标签页>用户控件>列表框&钮

那么,你如何通知形式深埋按钮被点击,然后填写表单中的列表框(或从形式调用用户控件来填充列表框)?

感谢大伙们。

Answer 1:

假设您的问题是有关的WinForms。

通知:暴露在用户控件的事件,并将其链接到按钮的情况下,形成知道它的孩子。

public class MyUserControl {
    private Button myButton;
    public event EventHandler MyControlButtonClicked;

    public MyUserControl() {
         ...
         myButton.Click += OnMyButtonClicked;
    }

    private void OnMyButtonClicked(object sender, EventArgs arguments) {
        if (MyControlButtonClicked != null) {
           MyControlButtonClicked(this, arguments);
        }
    }
}

在您的形式:

public class MyForm {
   private MyUserControl userControl;

   public MyForm() {
     ...
     userControl.MyControlButtonClicked += OnUserControlButtonClicked;
   }

   private void OnUserControlButtonClicked(object sender, EventArgs arguments) {
      // handle the button click here
   }
}

人口:同样的模式,用你的用户控件作为调解人。 添加上用户控件的公共方法,将做列表框的人口和从您的形式调用它。



文章来源: Communicating from/to usercontrol in tabcontrol in form