Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB10

Saving Settings

2.60/5 (4 votes)
21 Jun 2010CPOL 13K  
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)