I am trying to save a Collection of form controls (Labels and TextBoxes) in my project's settings (My.Settings). During runtime everything appears to be working as expected. I can save and retrieve the Collection of controls, but when I close and restart the application, the settings are gone. I am saving the settings (My.Settings.Save()) on the event handler and on closing of the app. Other settings (Strings and whatnot) work fine between runtimes.
So I guess my question is how come my Collection is not saving after I close the app?
INFO:
Settings where I save the Collection in project settings:
- defaultLayout (Microsoft.VisualBasic.Collection)
- currentLayout (Microsoft.VisualBasic.Collection)
Sub where I create and save controls into Collection setting:
Public Sub updateLayoutFromTable(ByVal tb As DataTable)
Try
RecordsForm.PanelAddRecord.Controls.Clear()
Dim yPos As Integer = 20
Dim xPos As Integer = 20
Dim curLabel As Label
Dim curBox As TextBox
My.Settings.defaultLayout = New Collection()
For i = 0 To tb.Rows.Count - 1 ' Rows
For j = 0 To tb.Columns.Count - 1 ' Columns
curLabel = New Label()
curBox = New TextBox()
curLabel.Location = New Point(xPos, yPos)
curLabel.AutoSize = True
curLabel.Text = tb.Rows.Item(i).Item(j).ToString()
curLabel.ForeColor = Color.Black
curLabel.BackColor = Color.White
curLabel.Name = tb.Rows.Item(i).Item(j).ToString()
My.Settings.defaultLayout.Add(curLabel)
curBox.Location = New Point(100, yPos)
curBox.Width = 200
curBox.Name = tb.Rows.Item(i).Item(j).ToString() & "Value"
curBox.Cursor = Cursors.Arrow
curBox.BackColor = Color.White
My.Settings.defaultLayout.Add(curBox)
yPos += 30
Next
Next
My.Settings.Save()
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
End Sub
Sub where I load the Collection of controls (just labels and textboxes):
Private Sub ButtonLoadDefaultLayout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLoadDefaultLayout.Click
Try
'add default layout to new record form
For Each c In My.Settings.defaultLayout
RecordsForm.PanelAddRecord.Controls.Add(c)
My.Settings.currentLayout.Add(c)
Next
My.Settings.Save()
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
End Sub
Sub that I call in my Load handler where I load (but fails) the Collection of controls:
Private Sub initLayout()
Try
If My.Settings.currentLayout IsNot Nothing Then
Debug.Print("setting default layout")
'load the layout
For Each c In My.Settings.currentLayout
RecordsForm.PanelAddRecord.Controls.Add(c)
Next
Else
Debug.Print("No layout detected")
End If
Catch ex As Exception
MessageBox.Show("Error: could not load layout" & vbNewLine & ex.ToString())
End Try
End Sub
I'm not getting any exception errors, but everytime the above Sub runs (on the Load event), the setting "My.Settings.currentLayout" is always Nothing. Even though the setting appears to work fine during runtime, it never saves for some reason. Can anyone shed light on what I am doing wrong or if I am misunderstanding how this works. I know I can create an XML file with all of the controls properties to load and recreate the form layout, but I would rather not go that route if possible. I like the ease of using the built-in settings of Visual Studio.
Thanks!!!