C#make父面板处理所有事件

时间:2012-08-19 16:24:31

标签: c# winforms events panel parent

当我遇到这个问题时,我正在创建一个自定义进度条[WinForm]

**The Structure:**
 -panel 
   -> panel
  so I have panel which inside the panel, there another panel.
**The Goals:**
-I want to use my parent panel as all event handler, 
 while make the child panel have no event at all.
**The Problem:**
- when I press my mouse inside the child panel. the event in parent wont called.
  explanation : -> I still wanted to call parent panel mouse down 
  even if I click on top of my child panel.

1 个答案:

答案 0 :(得分:1)

因此,您希望在单击子面板时在父面板上触发click事件。

我可以想到两种方法可以做到这一点。

第一种方法是从Panel2的Click事件处理程序方法中简单地调用Panel1的Click事件处理程序方法:

private void panel1_Click(object sender, EventArgs e)
{
    MessageBox.Show("Panel 1 clicked.");
}

private void panel2_Click(object sender, EventArgs e)
{
    this.panel1_Click(sender, e);
}

可能更好的方法是将两个点击事件都注册到1处理程序方法:

private void panel1_Click(object sender, EventArgs e)
{
    MessageBox.Show("Panel 1 clicked.");
}

然后从表单设计器或手动注册第二个面板的事件:

this.panel2.Click += new System.EventHandler(this.panel1_Click);