Saving Settings - xfgr.com Click here to Skip to main content

Saving Settings

Saving a form's settings
Sign Up to vote for this article
Add a reason or comment to your vote: x
1
answer
2.8/5
3 votes
530
views
Tags: VB7.x, VB8.0, VB9.0, VB10 ...

The aim is to get the properties of the Form and the controls in it and save it to a string called frmstring.
'Global Declaration
Dim frmstring as String 'The code below can be in any subroutine
Dim frm As Assembly = Assembly.LoadFile(Application.ExecutablePath)
For Each s As Type In frm.GetTypes
frmstring = frmstring & frm.GetName.FullName
For Each m As PropertyInfo In s.GetProperties
Try frmstring = frmstring & (m.Name & " : " & m.GetValue(Me, Nothing)) & vbCrLf
'm.Name is the property name. m.GetValue gets the value of that property of Me(The form)
Catch ex As Exception

End Try
Next
frmstring = frmstring & "------------------------" & vbCrLf
Next
For Each ctrl As Control In Me.Controls
frmstring = frmstring & ctrl.Name & vbCrLf
Dim z = ctrl.GetType()
'This is for enumerating the properties of that control.
For Each n As PropertyInfo In z.GetProperties
Try frmstring = frmstring & (n.Name) & " : " & n.GetValue(ctrl, Nothing).ToString & vbCrLf
'n.Name is the property name of the control. n.getvalue returns the value of that property of ctrl(the current control in the form's control list)
Catch ex As Exception

End Try
Next
Next
Posted 14 Jun '10 9:01 PM
pranav952.6K
Edited 21 Jun '10 3:44 AM
Revision 2
Reason for my vote of 2 1) Global vars = bad! 2) What are you supposed to do with the string? There's no restore function... - Johnny J. 15 Jun '10 2:22 AM
Restoring is simple. Scan the string line by line. Check if a colon exists in the current line. If it exists, it is a property. So, split it and get the two values. Use SetValue for that particular property. If there is no colon, then see if a control of that name exists. If it doesn't, skip to the next line - pranav95 15 Jun '10 11:25 PM
Reason for my vote of 2 you saved the FullName of the type and then right away saved the first property. How are you supposed to differentiate between the end of the type and the beginning of the first property? And what in the world is with the hyphens? Totally unnecessary. - William Winner 21 Jun '10 5:05 PM

0.00 / 5, No votes
Sign Up to vote for this article
Add a reason or comment to your vote: x
Here's an alternate: a custom class that is serializable to store all of that in instead of a single string that you'd then have to parse.

Or store it all in an XML file. There are plenty of object-oriented ways to go about this that would be much more readable than just storing it all in a string.

And I still want to see your code to get it back. Without being able to do that, being able to store it is pretty useless and only provides an incomplete Tip/Trick

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Hint: For improved responsiveness use Internet Explorer 4, Firefox or above. Ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
FAQ FAQ   
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Last Updated 26 Mar 2009
Web22 2.3.0027 Advertise Privacy