- Session State
- The SessionState Property
- Helper Routines
- Tracking Forms
- Gluing It All Together
The SessionState Property
Each of the Restore program's forms has a SessionState property. The property get procedure returns an XML string representing the form's session state. The property set procedure takes a saved session state and restores the form to that state. The code in Listing 1 shows how the program's main MDI form gets and sets its session state. Don't worry about how the MakeXmlNode and ChildNodeValue routines work just yet. They are described in the following sections.
Listing 1. The Restore Program's Main Form Uses this Code to Get and Set Its Session State
' Save or restore the form's session state. ' This form cares only about its position and size. Public Property SessionState() As String Get Dim doc As New XmlDocument() Dim root_node As XmlNode Dim data_node As XmlNode ' Create the root <MainForm> node. root_node = MakeXmlNode(doc, "MainForm") ' Save the size and position. MakeXmlNode(root_node, "X", Me.Location.X) MakeXmlNode(root_node, "Y", Me.Location.Y) MakeXmlNode(root_node, "Width", Me.Size.Width) MakeXmlNode(root_node, "Height", Me.Size.Height) ' Return the root node's XML text. Return root_node.OuterXml End Get Set(ByVal Value As String) Dim doc As New XmlDocument() Dim root_node As XmlNode ' Load the XML document from the string. doc.LoadXml(Value) ' Get the root data node. root_node = doc.DocumentElement ' Set the form's size and position. Me.SetBounds( _ ChildNodeValue(root_node, "X", Me.Location.X), _ ChildNodeValue(root_node, "Y", Me.Location.Y), _ ChildNodeValue(root_node, "Width", Me.Size.Width), _ ChildNodeValue(root_node, "Height", Me.Size.Height)) End Set End Property
The SessionState property get procedure returns the form's session state in XML format. It begins by making a new XmlDocument object to represent the XML document. It calls the MakeXmlNode subroutine, described in the next section, to add nodes to the document representing the form's size and position. It then returns the document's XML text. The following code shows a sample result.
<MainForm> <X>291</X> <Y>16</Y> <Width>714</Width> <Height>508</Height> </MainForm>
Here, I have added carriage returns and indentation to make the XML data easier to read. Normally, it appears all on one long line. Visual Basic provides objects that can add formatting to XML data, but it's not necessary for this program. See my book Visual Basic.NET and XML (Wiley, 2002, http://www.vb-helper.com/xml.htm) for information on using those classes.
The property set procedure shown in Listing 1 creates an XmlDocument object and uses its LoadXml method to load the document with the form's saved session state. It uses the ChildNodeValue function, described in the next section, to retrieve the form's position and size information, and uses those values to put the form back where it was when the session information was saved. Notice that the procedure uses the form's current size and position information as default values. If a value is missing from the saved session information, the form's corresponding property is unchanged.