4

I have a SplitContainer I want to catch the Panel2 collapsing and expanding events.

Any idea how to do that?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
AMH
  • 6,363
  • 27
  • 84
  • 135

4 Answers4

7

Posting this for others that might be hunting for the same answer as I was.

Unfortunately SplitContainer doesn't offer any direct events for the collapsed events. What I have found useful is to monitor the SizeChanged and/or ClientSizeChanged events of the OPPOSITE panel to the one you're collapsing.

Meaning, if I am interested in monitoring collapses of Panel2, I would subscribe to ClientSizeChanged events for Panel1.

In practice I would recommend monitoring the ClientSizeChanged for both panels of the SplitContainer to guarantee you don't miss any initialization or direct splitter movements.

In the below example I have a toggle button (btnToggle) that I want the Checked state to follow the visibility of Panel2 in the SplitContainer:

private void splitContainer_Panel2_ClientSizeChanged(object sender, EventArgs e)
{
        btnToggle.Checked = !splitContainer.Panel2Collapsed;
}

private void splitContainer_Panel1_ClientSizeChanged(object sender, EventArgs e)
{
        btnToggle.Checked = !splitContainer.Panel2Collapsed;
}
Sverrir Sigmundarson
  • 2,453
  • 31
  • 27
1
splitContainer.Panel1.VisibleChanged += (s, e) => { bool isPanel1Collapsed = splitContainer.Panel1Collapsed; };
spart
  • 121
  • 2
  • 4
1

In the internal implementation, when a panel in a SplitContainer is collapsed, is Visible property is set to false and vice-versa. It is therefore possible to detect the changes when a panel is collapsed by handling the VisibleChanged event of the desired panel.

The catch is that the SplitterPanel class doesn't expose this event. However, since it inherits the Panel class which exposes this event, you can cast to a Panel and handle the event from there as demonstrated in the sample code below.

private void Initialize()
{
    split = new SplitContainer();
    ((Panel)split.Panel1).VisibleChanged += splitPanel1_Collapsed;
}

private void splitPanel1_Collapsed(object sender, EventArgs e)
{
    var panel = (SplitterPanel)sender;
    var panelCollapsed = !panel.Visible;
}
Alex Essilfie
  • 12,339
  • 9
  • 70
  • 108
  • Thanks. The only problem with the above solution is that the "panel.Visible" syntax does not exist in my visual studio. – Alireza Apr 22 '19 at 12:44
  • Which version of visual Studio are you using? I have tested it in VS2015, VS2017 and VS2019 and it works with no modification. – Alex Essilfie Apr 23 '19 at 09:53
  • My visual studio version is 2005. Maybe I should switch to the newer version. Thanks for your attention. – Alireza Apr 24 '19 at 17:29
1

There isn't an event exactly for that, but that's because you should know when it's getting collapsed when the code it run:

splitContainer1.Panel1Collapsed = true;
// do your stuff

Otherwise, you can watch for the SplitterMoved or SplitterMoving events on the SplitContainer control.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • SplitterMoved work only when transfer from colapsed mode to uncolapse, SplitterMoving doesn't fire on the colapsing and uncolapsing any idea . I know Panel1Collapsed and Panel2Collapsed any idea – AMH Jan 04 '12 at 14:17
  • @AMH There isn't a way for the user to collapsed a panel, which means you have control of it. When you set `Panel1Collapsed=true;` then you should do whatever it is you want to do. It's hard to understand the problem. Maybe post some code of what you are trying to do (edit your question). – LarsTech Jan 04 '12 at 14:31