我在应用程序中使用拆分容器。一个父级拆分容器。父拆分容器的面板2中有三个拆分容器。
现在,当我点击父容器的panel2中嵌入的拆分容器的一个面板时,如何找到哪个面板被点击了?
提前感谢!
您可以尝试订阅所有面板以使用相同的单击事件。发送方将是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());
}
出于演示目的,我在每个面板的标记属性中输入了1或2,因为SplitContainer中使用的子面板不使用Name属性。
事件处理程序的sender
属性是单击的面板。
private void button1_Click(object sender, System.EventArgs e){
//sender is the panel which has just been clicked, cast it.
}