In my winform there are some Splitter to separate some datagridviews, is there a way to store (and recover) the splitter position into the user.config?
I wish avoid to add a setting with a different name for each splitter if it's possible.
thanks in advance
I came up with something you may be able to use. A few things about this example:
SplitContainer
, but I'd imagine you could adapt this pretty easily.SplitContainer
(you'll probably need to do that recursively).string
.I would personally recommend assigning names to your Splitter
s (or SplitContainer
s, depending on which type you're using) since that should shield you from the issues I mentioned.
In any event I hope this helps.
public Form1()
{
InitializeComponent();
Closing += Form1_Closing;
ApplySavedSplitterData();
}
void Form1_Closing(object sender, CancelEventArgs e)
{
SaveSplitterData();
}
private void SaveSplitterData()
{
Settings.Default.SplitterPositions = string.Join(";",
Controls.OfType<SplitContainer>()
.Select(s => s.SplitterDistance));
Settings.Default.Save();
}
private void ApplySavedSplitterData()
{
if (string.IsNullOrEmpty(Settings.Default.SplitterPositions))
{
return;
}
var positions = Settings.Default.SplitterPositions
.Split(';')
.Select(int.Parse).ToList();
var splitContainers = Controls.OfType<SplitContainer>().ToList();
for (var x = 0; x < positions.Count && x < splitContainers.Count; x++)
{
splitContainers[x].SplitterDistance = positions[x];
}
}