[2008] Nested Splitcontainers: Remember my Splitter positions
I have 3 splitcontainers on my form.
Each splitcontainer has vertical splitter orientation
This forms 3 'panels' which are resizable by dragging their respective splitters.
Each vertical 'panel' is then split horizontally by adding another splitcontainer with horizontal splitter orientation.
I now have 3 column 'panels' each of which is split into 2 'rows'
My question is :
In a nested splitcontainer scenario
When a user drags splitters and resizes panels,
how can I remember the last splitter location
so that when the form is closed and reopened,
it remembers the last used positions ?
If there is a better solution or way of achieving this, I am all ears.
Thanks for any help.
Re: [2008] Nested Splitcontainers: Remember my Splitter positions
You could create a new PropertyBinding setting for SplitterDistance and save the distance to this property everytime the form closes.
Re: [2008] Nested Splitcontainers: Remember my Splitter positions
use the built in ability for settings in your app.
In the project properties go to the settings tab, and add settings in there for whatever values you wish to remember. You could probably just make them integer types because you will be storing number values for positioning of these split containers.
Lets say I name my setting SomeValue, and make it an integer type (you can also give a default value if you want)
Then in code, you can simply use it like this:
My.Settings.SomeValue = whatever value you want
My.Settings.Save()
then when your app loads, you can access these values and position your splitters accordingly.
It works rather well, with little code on your end.
Re: [2008] Nested Splitcontainers: Remember my Splitter positions
Or do what he Keilnma says (thats twice ive said that today, maybe i should stop posting)
Re: [2008] Nested Splitcontainers: Remember my Splitter positions
Quote:
Originally Posted by kleinma
use the built in ability for settings in your app.
In the project properties go to the settings tab, and add settings in there for whatever values you wish to remember. You could probably just make them integer types because you will be storing number values for positioning of these split containers.
Thanks, that's Sweet. I'm off to experiment
Re: [2008] Nested Splitcontainers: Remember my Splitter positions
this is how
vb Code:
Public Class Form1
Dim loading As Boolean = True
Private Sub SplitContainer1_SplitterMoved(ByVal sender As Object, ByVal e As System.Windows.Forms.SplitterEventArgs) Handles SplitContainer1.SplitterMoved
If Not loading Then
My.Settings.splitter1 = e.SplitX
My.Settings.Save()
End If
End Sub
Private Sub SplitContainer2_SplitterMoved(ByVal sender As Object, ByVal e As System.Windows.Forms.SplitterEventArgs) Handles SplitContainer2.SplitterMoved
If Not loading Then
My.Settings.splitter2 = e.SplitX
My.Settings.Save()
End If
End Sub
Private Sub SplitContainer3_SplitterMoved(ByVal sender As Object, ByVal e As System.Windows.Forms.SplitterEventArgs) Handles SplitContainer3.SplitterMoved
If Not loading Then
My.Settings.splitter3 = e.SplitX
My.Settings.Save()
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SplitContainer1.SplitterDistance = My.Settings.splitter1
SplitContainer2.SplitterDistance = My.Settings.splitter2
SplitContainer3.SplitterDistance = My.Settings.splitter3
loading = False
End Sub
End Class
Re: [2008] Nested Splitcontainers: Remember my Splitter positions
that seems like a bit of overkill to save the settings everytime you move the splitter. I would just save them when you close the form. Likewise, if you go into project properties, you will notice the checkbox for "Save My.Settings on shutdown" is already checked, meaning you don't even really ever have to call My.Settings.Save() because it automatically happens when you close the program.
Re: [2008] Nested Splitcontainers: Remember my Splitter positions
not really. how often do you move the splitter?
Re: [2008] Nested Splitcontainers: Remember my Splitter positions
depends on the app. I constantly am moving splitters around in Visual Studio and outlook... 100s of times a day probably.
I guess it is up to preference and the app itself. Even if you did want to do it that way though, I would advise doing it in a single routine.
Code:
Private Sub AnySplitContainer_SplitterMoved( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.SplitterEventArgs) _
Handles SplitContainer1.SplitterMoved, _
SplitContainer2.SplitterMoved, _
SplitContainer3.SplitterMoved
With DirectCast(sender, SplitContainer)
If Not loading Then
My.Settings.Item(.Name) = e.SplitX
My.Settings.Save()
End If
End With
End Sub
Re: [2008] Nested Splitcontainers: Remember my Splitter positions
Kleinma, the settings... what should the 'scope' be set to in the project settings ?
Re: [2008] Nested Splitcontainers: Remember my Splitter positions
scope you want set to user. If you set it to application, the values become READONLY and can not be altered by your code.
Re: [2008] Nested Splitcontainers: Remember my Splitter positions
Quote:
Originally Posted by kleinma
that seems like a bit of overkill to save the settings everytime you move the splitter. I would just save them when you close the form.
Matt, thanks. What code would I use to save all splitter positions when I close the form ?
Re: [2008] Nested Splitcontainers: Remember my Splitter positions
Figured it out if anyone is interested...
First set your initial splitter values under project settings.
Code:
Public Class Form1
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
My.Settings.Splitter1 = SplitContainer1.SplitterDistance
My.Settings.Splitter2 = SplitContainer2.SplitterDistance
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SplitContainer1.SplitterDistance = My.Settings.Splitter1
SplitContainer2.SplitterDistance = My.Settings.Splitter2
End Sub
End Class
Re: [2008] Nested Splitcontainers: Remember my Splitter positions
Unfortunately, this does not work when you have Orientation set to "Horizontal". Still looking for that answer.