如何在鼠标单击时找到拆分容器中的哪个面板?

时间:2012-08-30 15:16:11

标签: c# winforms layout

我在我的应用程序中使用拆分容器。一个父分裂容器。父拆分容器的panel2中有三个拆分容器。

现在,当我点击父容器panel2中嵌入的拆分容器面板之一时,如何找到哪个容器被点击哪个容器?<​​/ p>

提前致谢!

2 个答案:

答案 0 :(得分:2)

您可以尝试订阅所有面板以使用相同的点击事件。发送者将是SplitterPanel类,它将具有一个Parent属性(隐藏在IDE中但它就在那里)将是SplitContainer:

public Form1() {
  InitializeComponent();

  splitContainer1.Panel1.MouseClick += Panel_MouseClick;
  splitContainer1.Panel2.MouseClick += Panel_MouseClick;
  splitContainer2.Panel1.MouseClick += Panel_MouseClick;
  splitContainer2.Panel2.MouseClick += Panel_MouseClick;
  splitContainer3.Panel1.MouseClick += Panel_MouseClick;
  splitContainer3.Panel2.MouseClick += Panel_MouseClick;
  splitContainer4.Panel1.MouseClick += Panel_MouseClick;
  splitContainer4.Panel2.MouseClick += Panel_MouseClick;
}

void Panel_MouseClick(object sender, MouseEventArgs e) {
  SplitterPanel sp = sender as SplitterPanel;
  SplitContainer sc = sp.Parent as SplitContainer;
  MessageBox.Show(sc.Name + " - " + sp.Tag.ToString());
}

出于演示目的,我在每个面板的tag属性中输入了1或2,因为SplitContainer中使用的子面板不使用Name proprety。

答案 1 :(得分:0)

事件处理程序的sender属性是单击的面板。

private void button1_Click(object sender, System.EventArgs e){
    //sender is the panel which has just been clicked, cast it.
}