Results 1 to 14 of 14

Thread: [2008] Nested Splitcontainers: Remember my Splitter positions

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    515

    [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.

  2. #2
    Hyperactive Member mrmojorisin's Avatar
    Join Date
    Oct 2007
    Location
    London Town Vocation: Garden Cricket Genuis
    Posts
    439

    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.

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  4. #4
    Hyperactive Member mrmojorisin's Avatar
    Join Date
    Oct 2007
    Location
    London Town Vocation: Garden Cricket Genuis
    Posts
    439

    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)

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    515

    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

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [2008] Nested Splitcontainers: Remember my Splitter positions

    this is how

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim loading As Boolean = True
    4.  
    5.     Private Sub SplitContainer1_SplitterMoved(ByVal sender As Object, ByVal e As System.Windows.Forms.SplitterEventArgs) Handles SplitContainer1.SplitterMoved
    6.         If Not loading Then
    7.             My.Settings.splitter1 = e.SplitX
    8.             My.Settings.Save()
    9.         End If
    10.     End Sub
    11.  
    12.     Private Sub SplitContainer2_SplitterMoved(ByVal sender As Object, ByVal e As System.Windows.Forms.SplitterEventArgs) Handles SplitContainer2.SplitterMoved
    13.         If Not loading Then
    14.             My.Settings.splitter2 = e.SplitX
    15.             My.Settings.Save()
    16.         End If
    17.     End Sub
    18.  
    19.     Private Sub SplitContainer3_SplitterMoved(ByVal sender As Object, ByVal e As System.Windows.Forms.SplitterEventArgs) Handles SplitContainer3.SplitterMoved
    20.         If Not loading Then
    21.             My.Settings.splitter3 = e.SplitX
    22.             My.Settings.Save()
    23.         End If
    24.     End Sub
    25.  
    26.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    27.         SplitContainer1.SplitterDistance = My.Settings.splitter1
    28.         SplitContainer2.SplitterDistance = My.Settings.splitter2
    29.         SplitContainer3.SplitterDistance = My.Settings.splitter3
    30.         loading = False
    31.     End Sub
    32.  
    33. End Class

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [2008] Nested Splitcontainers: Remember my Splitter positions

    not really. how often do you move the splitter?

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    515

    Re: [2008] Nested Splitcontainers: Remember my Splitter positions

    Kleinma, the settings... what should the 'scope' be set to in the project settings ?

  11. #11
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    515

    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 ?
    Last edited by Xancholy; Jul 2nd, 2008 at 11:23 AM.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    515

    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
    Last edited by Xancholy; Jul 2nd, 2008 at 11:51 AM.

  14. #14
    New Member
    Join Date
    Jul 2009
    Posts
    5

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width