65.9K
CodeProject is changing. Read more.
Home

Save your WinForms User Control Data to XML

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.80/5 (4 votes)

Sep 1, 2006

CPOL

4 min read

viewsIcon

59802

downloadIcon

1297

Allows the developer to save all of the data in user controls in a WinForms app

Introduction

Since this is my first attempt at an article, I would ask that you please be gentle with your comments. ;-)


This class gives the developer the ability to save all of the data on a windows form to an XML file stored on the user's local machine. This would be useful if the user was entering alot of duplicated data in a large form and wanted to save the information to prevent having to type it out each time.

Background

I actually wrote this code in 2005 to handle a request from a user. She said that she wanted to be able to save all of the data on her form to an XML file and have it automatically read back in when she opened up the form the next time.


Although this class only saves information for a windows form, I would not think it to be too difficult to save the same type of info for a web form. It would be a little difficult determining where to save the file full of XML data, but it could be done. Please, if you want to do that, feel free to modify my code and add the code to this class and post it back up here.

Using the code

There are a couple of procedures that the developer needs to use. To have this class work properly, you will need to make sure that you set the properties of the class to "True" for each type of control that you want the data saved as.

#Region " Public Properties"

    Public Property SaveTextBox() As System.Boolean
        Get
            Return mbSaveTextBox
        End Get
        Set(ByVal value As System.Boolean)
            mbSaveTextBox = value
        End Set
    End Property

    Public Property SaveComboBox() As System.Boolean ...

    Public Property SaveRadioButton() As System.Boolean ...

    Public Property SaveCheckBox() As System.Boolean ...

#End Region

As you can see, the properties are very simple and only meant to turn on/off the ability for the code to save that type of control's data. There are plenty of other controls that could be added here, but I have had no need for them yet.


Let's take a look at the code that is actually used to save and load the XML from the filesystem. I found that we had some initial trouble with controls that were contained in frames (not the ones for websites, but the frame control for WinForms). Check it out...

Public Sub SaveData(ByVal oParentForm As System.Windows.Forms.Form)

          msFileName = CType(Application.LocalUserAppDataPath.ToString &_
"\" & oParentForm.Name.ToString & "_FormData.xml", System.String) If File.Exists(msFileName) Then Try File.Delete(msFileName) Catch ex As Exception MessageBox.Show(ex.Message) End Try End If

First, we set a filename to the AppPath's file and the name of the form that we are looking for. We can then use that filename to process each control on the form and save the data, if we set it up to do so. Then, we open the XMLTextWriter to start writing the document...


          oTextWriter = New XmlTextWriter(msFileName, _
System.Text.Encoding.UTF8) With oTextWriter .WriteStartDocument(True) .WriteStartElement("Controls") End With

Next, we will iterate through the entire form's controls collection and see if we are supposed to save the data. If we are, we write the line to the XMLTextWriter.


          For Each oControl In oParentForm.Controls

              If TypeOf oControl Is Panel Or TypeOf oControl Is GroupBox Then
                  Dim oContainer As Control
                  Dim oChild As Control
                  oContainer = oControl

                  For Each oChild In oContainer.Controls
                      If TypeOf oChild Is GroupBox Then

                          Dim oGroupBox As Control
                          oGroupBox = oChild
                          Dim ochild2 As Control

                          For Each ochild2 In oGroupBox.Controls
                              checksavecontrol(ochild2)
                          Next
                      Else
                          checksavecontrol(oChild)
                      End If

                  Next

              Else
                  checksavecontrol(oControl)
              End If
          Next
   

The CheckSaveControl() function will determine if the control type's data should be saved to the file system. If it is, it writes the data to the XMLTextWriter.


After we have looked at all of the controls on the form, we close the XMLDocument up and close out the XMLTextWriter.


           With oTextWriter
               .WriteEndElement()
               .WriteEndDocument()
               .Flush()
               .Close()
        End With
   

The load function took a little more work. We have to iterate through all of the controls on the form as well, but we also have to make sure that they are not container controls. If they ARE containers, we have to loop through the container's controls collection and save that data as well.


This posed a problem when I first worked on it. We had a form that was trying to save the data and the control was setup to be saved, but the data wouldn't work. It turned out that we had nested container controls. There was a group box inside of a frame. This gave me a headache for hours until I noticed on the form's Design View what was going on! Keep your eye out for those nested container controls!

Possible Enhancements

Like I said, you could potentially use this in an Web app environment using a user's GUID instead of the Application.LocalUserAppPath and save the file to a directory that holds all of the user data.


If you come up with anything that needs corrected, let me know!  Thanks to GlimmerMan for pointing out that I forgot to finish the Load Event!!