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!!!

Did you take a look in to the generated user.config?
It is usually stored somewhere like:
C:\Users\<username>\AppData\Local\<company name>\<application name>.vshos_Url_<some random string>\1.0.0.0

Taking a look in to this file after calling the method updateLayoutFromTable
shows this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <WindowsApplication1.My.MySettings>
            <setting name="defaultLayout" serializeAs="Xml">
                <value />
            </setting>
        </WindowsApplication1.My.MySettings>
    </userSettings>
</configuration>

I would suggest you create your own XML file and save the layout in it.
Here an example:

<?xml version="1.0" encoding="utf-8" ?>
<Settings>
	<defaultLayout>
		<labels>
		<item name="lbl1" locX="20" locY="20" autosize="true" forecolor="black" backcolor="white"/>
		</labels>
		<textboxes>
			<item name="txt1" locX="100" locY="20" width="200"  forecolor="black" backcolor="white"/>
		</textboxes>
	</defaultLayout>
	<currentLayout/>
</Settings>

Did you take a look in to the generated user.config?
It is usually stored somewhere like:
C:\Users\<username>\AppData\Local\<company name>\<application name>.vshos_Url_<some random string>\1.0.0.0

Taking a look in to this file after calling the method updateLayoutFromTable
shows this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <WindowsApplication1.My.MySettings>
            <setting name="defaultLayout" serializeAs="Xml">
                <value />
            </setting>
        </WindowsApplication1.My.MySettings>
    </userSettings>
</configuration>

I would suggest you create your own XML file and save the layout in it.
Here an example:

<?xml version="1.0" encoding="utf-8" ?>
<Settings>
	<defaultLayout>
		<labels>
		<item name="lbl1" locX="20" locY="20" autosize="true" forecolor="black" backcolor="white"/>
		</labels>
		<textboxes>
			<item name="txt1" locX="100" locY="20" width="200"  forecolor="black" backcolor="white"/>
		</textboxes>
	</defaultLayout>
	<currentLayout/>
</Settings>

Thanks for your reply. I have taken a look at the user.config file, and the value for my layout settings (defaultLayout and currentLayout) remain empty. I'm trying to avoid creating and managing an XML file for the layout partially out of laziness, but mostly because it's just hard for me to believe that these settings work fine during runtime but won't work between runtimes. There has to be a way to save these settings for it to work and it is driving me nuts. I cannot find any documentation or any help online with this.

Has anyone out there ever used the project settings to store a collection of controls? I've got a form that the user can customize and save as a layout, but I just cannot seem to save it for use in another subsequent runtime.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.