If you can save the relevant control information in a list of strings you
can persist it into a string array in user settings. The control name
should not be saved, as you really need the control to exist before updating
it with the specific user information recovered from Settings, therefore
names should be generated according to an internal rule. I just use a
simple sequential form - User Panel 1, User Panel 2, etc.
Note that I am assuming you are dealing with one control type - it may be
possible with different control types, but a lot more complex. I can
therefore put each control in an array of controls (I have used A() in this
example) instead of referring to them through Me.Controls(). This means I
can access the controls by array index in A(), so I don't need to know the
names, and it means that there is no problem dealing with a variable number
of controls. My control is actually a user control (UserPanel) that
consists of a number of standard controls.
Set up your Settings storage like this - one for each property you need to
save.
User Setting Name - whatever (eg PANELTOPS)
User Setting Type - System.Collections.SpecializedStringCollection
User Setting Scope - User
User Setting Value -
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfString xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="
http://www.w3.org/2001/XMLSchema">
<string>25</string>
</ArrayOfString>
(25 is the actual default setting - change to your required default for each
particular list)
Then access it using code like the following:
'At Class level
Dim a As New List(Of UserPanel) 'Array of user panels.
'To save the Top of each control on a form:
Dim sp As New System.Collections.Specialized.StringCollection()
For N As Integer = 0 To A.Count - 1
sp.Add(A.(N).Location.Y.Tostring)
Next
My.Settings.PANELTOPS = sp
For N As Integer = 0 To A.Count - 1
sp.Add(A.(N).Location.X.Tostring)
Next
My.Settings.PANELLEFTS = sp
'etc
My.Settings.Save()
'To Retrieve and set the location of each control on a form
Dim UP as New UserPanel
Dim sp As New System.Collections.Specialized.StringCollection()
sp = My.Settings.PANELTOPS
For N As Integer = 0 To My.Settings.PANELTOPS.Count- 1
me.Controls.Add(UP)
a.add(UP)
'Other initialisation code for UP goes here
a(N).Location.Y = Val(sp(N))
Next
For N As Integer = 0 To My.Settings.PANELLEFTS.Count- 1
sp = My.Settings.PANELLEFTS
a(N).Location.X = Val(sp(N))
Next
'etc.