Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How do I keep SplitContainer.SplitterDistance from changing when the control/form its in is resized while still alowing the user to manually resize it?
Posted
Updated 9-Mar-11 5:53am
v2

One way to do it is to have a class variable to independantly hold the current position of the splitter and a flag to indicate if the mouse button is down:
C#
int split_distance = 150;
bool mouse_down = false;

Then add the following event handlers to the split container:
C#
private void splitContainer1_Layout ( object sender, LayoutEventArgs e )
{
     this.splitContainer1.SplitterDistance = this.split_distance;
}

private void splitContainer1_MouseDown ( object sender, MouseEventArgs e )
{
     this.mouse_down = true;
}

private void splitContainer1_MouseUp ( object sender, MouseEventArgs e )
{
      this.mouse_down = false;
}

private void splitContainer1_SplitterMoving ( object sender, SplitterCancelEventArgs e )
{
       if ( this.mouse_down )
       {
           this.split_distance = e.SplitX;
       }
}
 
Share this answer
 
v2
Comments
Dan Neely 9-Mar-11 13:29pm    
And here I was thinking I was blind for not being able to find a property that had to be there...
JOAT-MON 9-Mar-11 14:04pm    
Yeah, I re-read the property list several times to no avail, then I tried a bunch of combinations of the properties that seemed relevant and none seemed to work out, so I threw this together.
Dan Neely 9-Mar-11 14:44pm    
I've ran into one problem with this. On the left side I have a tab control with datagridview's on each tab. If I resize the form the splitter is on to be smaller the datagridview on the displayed tab doesn't paint the area that was hidden before the Layout event increased the splitter distance again. This is happening both when the control is docked (fill) or anchored (4 sides) in the tab page.

It's also sub-optimal in that what I'm seeing is the form initially painting with the splitter bar moved and then repainting itself when the bar is restored to its old position.
Dan Neely 9-Mar-11 14:52pm    
Apparently we're both blind. The FixedPanel property does exactly what I want. :doh:
JOAT-MON 9-Mar-11 15:05pm    
The saddest part is, I saw the property (didn't read the description) and thought to myself 'That will prevent the splitter from moving at all..that's not what he is looking for.' :doh:
The FixedPanel property allows setting one panel not to change size when the SplittContainer is resized.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900