Saving and retrieving WinForm template info

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

Is there a way to save the following info about controls on a winform to a
named file and then retrieve them back when needed?

Control Name, Position on form, Control Binding Info

The idea is to have different layouts of the controls on the form for
different users and load appropriate layout/template for a user from file
when needed.

Thanks

Regards
 
John,

Yes with a lot of work and probably very bad maintainable.

But you can use for that (in sequence of my preference) the registry, an
isolated storage, an XML file, a CSV file, a INI file maybe even more.

Cor
 
Hello Cor Ligthert[MVP],
John,

Yes with a lot of work and probably very bad maintainable.

But you can use for that (in sequence of my preference) the registry,
an isolated storage, an XML file, a CSV file, a INI file maybe even
more.

The Settings object comes to mind... it even has properties to store information
on a per user basis and can be used to databind their respective values to
the controls.

Probably the least amount of work.
 
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.
 
Back
Top