在TabControl中以表格形式与用户控件进行通讯 [英] Communicating from/to usercontrol in tabcontrol in form

查看:80
本文介绍了在TabControl中以表格形式与用户控件进行通讯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为C#很难.尝试在stackoverflow中发布问题.

I thought C# was hard. Try posting a question in stackoverflow.

我在一个用户控件中有一个列表框和一个按钮,它本身在tabcontrol的选项卡中,本身在窗体上.单击按钮时,我需要从表单中填充列表框.

I have a listbox and a button in a usercontrol, itself in a tabpage of a tabcontrol, itself on a form. I need to populate the listbox from the form when the button is clicked.

form> tabcontrol> tabpage> usercontrol>列表框&按钮

form > tabcontrol > tabpage > usercontrol > listbox & button

那么,如何通知表单已单击一个深埋按钮,然后从表单填充列表框(或从表单调用用户控件以填充列表框)?

So, how do you notify the form that a deeply buried button has been clicked and then fill the listbox from the form (or call the usercontrol from the form to populate the listbox)?

谢谢你们.

推荐答案

假设您的问题与WinForms有关.

Assuming that your question is about WinForms.

有关通知: 在userControl上公开一个事件,并将其链接到按钮的事件,窗体知道它是孩子的​​.

For notification: Expose an event on the userControl and link it to the event of the button, form knows it's children.

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
   }
}

对于人口: 使用相同的模式,将用户控件用作中介.在userControl上添加一个公共方法,该方法将填充listBox并从您的表单中调用它.

For population: The same pattern, use your user control as a mediator. Add a public method on userControl that will do the listBox population and call it from your form.

这篇关于在TabControl中以表格形式与用户控件进行通讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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