【问题标题】:Windows Forms: User controls and eventsWindows 窗体:用户控件和事件
【发布时间】:2010-10-04 13:19:31
【问题描述】:

我有一个由多个控件组成的 Windows 窗体 用户控件。除其他外,还有gridview 控件。现在我想在表单中使用用户控件。例如,如何检查是否单击了网格行?我想获取所选行的数据。 换句话说,如何检测嵌入在用户控件中的控件的事件?

【问题讨论】:

    标签: .net winforms user-controls


    【解决方案1】:

    您需要通过添加其他事件来公开父控件中的事件:

    public event EventHandler GridViewClicked;
    

    然后您使用以下命令在您的子控件上调用它:

    private void ChildControlGrid_RowClicked(object sender, EventArgs e)
    {
        if (GridViewClicked != null)
        {
            GridViewClicked(sender, e);
        }
    }
    

    然后,您可以像在表单上的任何事件一样实现它:

    yourControl.GridViewClicked += new EventHandler(ChildGridRowClicked);
    
    private void ChildGridRowClicked(object sender, EventArgs e)
    {
        // Child row clicked
    }
    

    【讨论】:

      【解决方案2】:

      在您的用户控件上创建一个新事件并使用它来公开 gridview 的事件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-10
        相关资源
        最近更新 更多