Home > Articles > Web Services > XML

  • Print
  • + Share This
From the author of

CustomerList.SessionState

A form's session state does not necessarily include every last piece of information about the form. The CustomerList form's session state includes its size and position, and the values in the form's combo boxes and text boxes. It does not include the list of customers selected when the user clicked the List button. When the user restarts the program, that list will be out of date. The user will need to click List again to make a new list, so there's no point in saving the old values.

Listing 3 shows the CustomerList form's SessionState property procedures. These are similar to the procedures used by the main form, except they save and restore more information.

Listing 3. The CustomerList Form Uses These SessionState Property Procedures

Public Property SessionState() As String
  Get
    Dim doc As New XmlDocument()
    Dim root_node As XmlNode
    Dim data_node As XmlNode
    Dim ctl As Control

    ' Create the root <CustomerListForm> node.
    root_node = MakeXmlNode(doc, "CustomerListForm")

    ' See if this is the MDI child ActiveForm.
    If Me.ParentForm.ActiveMdiChild Is Me Then
      MakeXmlNode(root_node, "ActiveForm")
    End If

    ' 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)

    ' Save the field selections.
    If cboField1.Text <> "" Then MakeXmlNode(root_node, "Field1", cboField1.Text)
    If cboField2.Text <> "" Then MakeXmlNode(root_node, "Field2", cboField2.Text)
    If cboField3.Text <> "" Then MakeXmlNode(root_node, "Field3", cboField3.Text)
    If cboField4.Text <> "" Then MakeXmlNode(root_node, "Field4", cboField4.Text)

    ' Save the operator selections.
    If cboOperator1.Text <> "" Then _
      MakeXmlNode(root_node, "Operator1", cboOperator1.Text)
    If cboOperator2.Text <> "" Then _
      MakeXmlNode(root_node, "Operator2", cboOperator2.Text)
    If cboOperator3.Text <> "" Then _
      MakeXmlNode(root_node, "Operator3", cboOperator3.Text)
    If cboOperator4.Text <> "" Then _
      MakeXmlNode(root_node, "Operator4", cboOperator4.Text)

    ' Save the field values.
    If txtValue1.Text <> "" Then MakeXmlNode(root_node, "Value1", txtValue1.Text)
    If txtValue2.Text <> "" Then MakeXmlNode(root_node, "Value2", txtValue2.Text)
    If txtValue3.Text <> "" Then MakeXmlNode(root_node, "Value3", txtValue3.Text)
    If txtValue4.Text <> "" Then MakeXmlNode(root_node, "Value4", txtValue4.Text)

    ' 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))

    ' Set the field selections.
    cboField1.Text = ChildNodeValue(root_node, "Field1", "")
    cboField2.Text = ChildNodeValue(root_node, "Field2", "")
    cboField3.Text = ChildNodeValue(root_node, "Field3", "")
    cboField4.Text = ChildNodeValue(root_node, "Field4", "")

    ' Set the operator selections.
    cboOperator1.Text = ChildNodeValue(root_node, "Operator1", "")
    cboOperator2.Text = ChildNodeValue(root_node, "Operator2", "")
    cboOperator3.Text = ChildNodeValue(root_node, "Operator3", "")
    cboOperator4.Text = ChildNodeValue(root_node, "Operator4", "")

    ' Set the entered text values.
    txtValue1.Text = ChildNodeValue(root_node, "Value1", "")
    txtValue2.Text = ChildNodeValue(root_node, "Value2", "")
    txtValue3.Text = ChildNodeValue(root_node, "Value3", "")
    txtValue4.Text = ChildNodeValue(root_node, "Value4", "")
  End Set
End Property

One piece of information that stands out is the ActiveForm entry. If this form is the main form's active MDI child, the property get procedure adds an empty tag named ActiveForm to the session state. The form's SessionState property set procedure does not read this value. The main program uses this value to determine which of its MDI children should be active when it restores the session settings.

The following code shows a sample SessionState for this form. The values are for the session shown in Figure 1. Again, I have added carriage returns and indentation to make the result easier to read.

<CustomerListForm>
  <ActiveForm />
  <X>0</X>
  <Y>0</Y>
  <Width>701</Width>
  <Height>227</Height>
  <Field1>Company Name</Field1>
  <Field2>City</Field2>
  <Operator1>LIKE</Operator1>
  <Operator2>=</Operator2>
  <Value1>%Inc%</Value1>
  <Value2>Boulder</Value2>
</CustomerListForm>
  • + Share This
  • 🔖 Save To Your Account