Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when SplitContainer collapsed changes

Tags:

c#

winforms

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

Any idea how to do that?

like image 415
AMH Avatar asked Dec 28 '22 08:12

AMH


1 Answers

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;
}
like image 149
Sverrir Sigmundarson Avatar answered Dec 29 '22 20:12

Sverrir Sigmundarson