tags:

views:

1201

answers:

5

I'm writing a WinForms application and one of the tabs in my TabControl has a SplitContainer. I'm saving the SplitterDistance in the user's application settings, but the restore is inconsistent. If the tab page with the splitter is visible, then the restore works and the splitter distance is as I left it. If some other tab is selected, then the splitter distance is wrong.

+2  A: 

I found the problem. Each tab page doesn't get resized to match the tab control until it gets selected. For example, if the tab control is 100 pixels wide in the designer, and you've just set it to 500 pixels during load, then setting the splitter distance to 50 on a hidden tab page will get resized to a splitter distance of 250 when you select that tab page.

I worked around it by recording the SplitterDistance and Width properties of the SplitContainer in my application settings. Then on restore I set the SplitterDistance to recordedSplitterDistance * Width / recordedWidth.

Don Kirkby
+3  A: 

There`s a more easy solution. If Panel1 is set as the fixed panel in SplitContainer.FixedPanel property it all behaves as expected.

Interesting. I'll have to experiment with it.
Don Kirkby
A: 

Don, your answer solved my problem thanks.

+1  A: 

For handling all cases of FixedPanel and orientation, something like the following should work:

        var fullDistance = 
           new Func<SplitContainer, int>(
               c => c.Orientation == 
                  Orientation.Horizontal ? c.Size.Height : c.Size.Width);

        // Store as percentage if FixedPanel.None
        int distanceToStore =
           spl.FixedPanel == FixedPanel.Panel1 ? spl.SplitterDistance :
           spl.FixedPanel == FixedPanel.Panel2 ? fullDistance(spl) - spl.SplitterDistance :
           (int)(((double)spl.SplitterDistance) / ((double)fullDistance(spl))) * 100;

Then do the same when restoring

        // calculate splitter distance with regard to current control size
        int distanceToRestore =
           spl.FixedPanel == FixedPanel.Panel1 ? storedDistance:
           spl.FixedPanel == FixedPanel.Panel2 ? fullDistance(spl) - storedDistance :
           storedDistance * fullDistance(spl) / 100;
Robert Jeppesen
A: 

Robert, your answer worked greatfull for me. Thanks!!

Tate