5

My Winforms app saves and restores its GUI state in the database. Everything's working ok, except for a Split Container's SplitterDistance.

The value is correctly loaded and set from DB, but when I exit the app without touching the splitter, I expect it to save the same value. But it saves the initial value minus 25 pixels. If I open and close the app many times, the splitter distance decreases by 25 pixels every time.

It's not a custom control, just a plain old .NET SplitContainer. The control is only accessed programatically to load its initial SplitterDistance and save it on exit, nothing else.

How can I troubleshoot this?

UPDATE: The spl's FixedPanel property was originally set to None. Tried setting it to Panel1 and Panel2; in both cases, SplitterDistance grows 50 pixels when I save it.

dario_ramos
  • 7,118
  • 9
  • 61
  • 108

4 Answers4

2

Do you have user controls inside the split container , and probably when they are all loaded it would resized along with the user controls which contain it.

To troubleshoot that, set a FixedPanel Property and observe it.

Turbot
  • 5,095
  • 1
  • 22
  • 30
  • See my update. Could it that the whole split container is resized? – dario_ramos Jul 19 '12 at 19:53
  • Added a handler to the resize event and, yes, the container is being resized. So now I think I should set the SplitterDistance from DB once I'm sure .NET is done resizing everything (when?). – dario_ramos Jul 19 '12 at 20:12
  • do you have equal width of your spliter container, and what is your window size when you load into the screen ? – Turbot Jul 19 '12 at 20:16
  • I solved it. After I set the SplitterDistance with the value from DB, a resize event is fired which changes the split container's size and SplitterDistance. Using the call stack, I found out that another control which is above the split container is being resized. If I set the SplitterDistance after handling the other control, everything works fine. I'm accepting your answer because it pointed me in the right direction. – dario_ramos Jul 19 '12 at 20:22
  • I think that's the reason I put "Observe it" :) , glad you solved it. – Turbot Jul 19 '12 at 20:24
  • I set all my SplitterDistances in the Form.Shown event after all the other controls have finished working out their positions, etc. – ourmandave Feb 10 '21 at 13:41
2

download sample application

        // my splitContainer1 is Horizontal so i used splitContainer1.ClientSize.Height 
        // if you have splitContainer1 is Vertical use splitContainer1.ClientSize.Width
        // without FixedPanel save and load """  
        // loading SplitterDistance from ini file
        int splitContainery = 0;
        splitContainery = (Win32.GetPrivateProfileInt(PluginName, "splitContainer", 0, iniFilePath));
        splitContainer1.SplitterDistance = splitContainer1.ClientSize.Height - splitContainery;

        // saving splitContainer1.SplitterDistance to ini file
        int hhkt = splitContainer1.ClientSize.Height - splitContainer1.SplitterDistance;
        Win32.WritePrivateProfileString(PluginName, "splitContainer", hhkt.ToString(), iniFilePath);
  • 1
    Would be great to include a sentence or two about how your solution solves the question asked. – Ken Jan 20 '19 at 19:40
1

I used the above response as follows (my splitter is vertical):

private void Form1_Load(object sender, EventArgs e) 
{
    int splitContainery = Properties.Settings.Default.SplitterDistance;
    if(splitContainery < splContainer.ClientSize.Width)
        splContainer.SplitterDistance = splContainer.ClientSize.Width - splitContainery;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    int splitContainery = splContainer.ClientSize.Width - splContainer.SplitterDistance;
    Properties.Settings.Default.SplitterDistance = splitContainery;
    Properties.Settings.Default.Save();            
}

I created the PropertyBinding for the splContainer.SplitterDistance, but did not bind it to the property, just to have it in the ApplicationSettings file.

Adail Retamal
  • 428
  • 5
  • 10
-1
int splitContainery = Properties.Settings.Default.SplitterDistance;

if i could use Properties.Settings.Default.SplitterDistance so i could never use ini file for example

Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26