我在我的应用程序中使用拆分容器。一个父拆分容器。父拆分容器的 panel2 中有 3 个拆分容器。
现在,当我单击嵌入在父容器的 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());
}
出于演示目的,我在每个面板的 tag 属性中输入了 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.
}