Click here to Skip to main content
15,883,809 members
Articles / Programming Languages / Visual Basic 10
Tip/Trick

Saving Settings

Rate me:
Please Sign up or sign in to vote.
2.60/5 (4 votes)
21 Jun 2010CPOL 12.1K   2   4
Saving a form's settings
The aim is to get the properties of the Form and the controls in it and save it to a string called frmstring.
VB
'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

License

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


Written By
Student
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy reason for a vote of 2 1. You trap for errors and do no... Pin
Simon_Whale14-Jul-10 1:08
Simon_Whale14-Jul-10 1:08 
GeneralReason for my vote of 2 you saved the FullName of the type a... Pin
William Winner21-Jun-10 11:05
William Winner21-Jun-10 11:05 
GeneralRestoring is simple. Scan the string line by line. Check i... Pin
Anshul R15-Jun-10 17:25
Anshul R15-Jun-10 17:25 
GeneralReason for my vote of 2 1) Global vars = bad! 2) What are y... Pin
Johnny J.14-Jun-10 20:22
professionalJohnny J.14-Jun-10 20:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.