I have been tasked to make the UI of a Winforms application written in VB to save the position of several SplitContainer splitters and the window size. I will show the code for the SplitContainers below. The code for the window is very similar but addressing different properties. Note that all of the SplitContainer values are being saved as Integer and assigned to the User scope.
The code is pretty straight forward. When the form loads I check My.Settings.SettingsLoaded which defaults to False. If it is False, I grab the current default position and write save them.
Private Sub InitSettings()
If My.Settings.SettingsLoaded <> True Then
UpdateWindowSettingsData()
UpdateSplitContainerSettingsData()
My.Settings.SettingsLoaded = True
My.Settings.Save()
End If
isLoading = False
ScaleWindow()
ScaleUIElements()
End Sub
The second part of that runs every time the form loads and positions the elements in question
Private Sub ScaleUIElements()
isLoading = True
SuspendLayout()
SplitContainer3.SplitterDistance = My.Settings.SplitContainer3
SplitContainer8.SplitterDistance = My.Settings.SplitContainer8
SplitContainer10.SplitterDistance = My.Settings.SplitContainer10
SplitContainer20.SplitterDistance = My.Settings.SplitContainer20
SplitContainer21.SplitterDistance = My.Settings.SplitContainer21
ResumeLayout()
isLoading = False
End Sub
Then, I have attached several handlers to catch the user manipulations
Private Sub SplitterMoved(ByVal sender As System.Object, ByVal e As System.Windows.Forms.SplitterEventArgs) Handles SplitContainer3.SplitterMoved, SplitContainer8.SplitterMoved, SplitContainer20.SplitterMoved, SplitContainer21.SplitterMoved, SplitContainer10.SplitterMoved
If isLoading Then
Return
End If
UpdateSplitContainerSettingsData()
End Sub
The updating of the data is also pretty straight forward
Private Sub UpdateSplitContainerSettingsData()
My.Settings.SplitContainer3 = SplitContainer3.SplitterDistance
My.Settings.SplitContainer8 = SplitContainer8.SplitterDistance
My.Settings.SplitContainer10 = SplitContainer10.SplitterDistance
My.Settings.SplitContainer20 = SplitContainer20.SplitterDistance
My.Settings.SplitContainer21 = SplitContainer21.SplitterDistance
My.Settings.Save()
End Sub
As I work on this I have been monitoring the user.config file using Tail.exe. This allows me to see the settings update as they are saved. I even went as far as to set it up so that a sound plays when the document updates.
What I am seeing is that as I move the splitters around in the SplitContainers, I can see the events fire as they should by setting breakpoints. However, I can also watch it hit the break point, update the settings, run past the save line, and absolutely not update the document. It works about %40 of the time and seems to be completely random. I have put almost a full day into trying to get this to work and am in the exact same place I was when I wrote the code initially. I can't find anything indicating anyone has ever seen this behavior before and I am confident that my code is working as it should, but for some reason the app is not able to carry out the write to the file.
I have a pretty strong indication this is true. When My.Settings.Save() is called and the values do not update in Tail.exe, the bottom left corner status text says 'Waiting for file...'. When it does work, the text says 'Last Updated: XX:XX:XX'(timestamp). Waiting for file will never go away if I just leave it.
So, I was wondering if anyone else has ever encountered similar behavior. If so, how can I cause the settings to finish writing? Would I be better off using a 3rd party solution? Of course, if you can spot anything I am doing wrong when saving these settings, please let me know.
Thanks for any help!