1. Home
  2. Computing & Technology
  3. Visual Basic

Part 6: TripPlanner - A Complete WPF and XAML in Visual Basic
The TripDesc WPF Form

By Dan Mabbutt, About.com

Nov 2 2008

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

In this fairly standard form, the Value properties of the XML elements are used to update the Text properties of the WPF components after the WPF form is loaded. This demonstrates the corresponding properties in the two new ways of containing data in WPF and XML. The WPF TextBox components are updated when one loses the focus. The Text property may not have changed, but this makes the code a lot simpler.

The only other code element that might need explaining is that a worker class - TripInfo.vb - is used to actually save the XML. This is also done simply to avoid duplicated code. In terms of execution time, it would probably be more efficient to have the code duplicated but I chose this method.

Partial Public Class TripDesc
   Dim MyTrip As TripInfo = New TripInfo
   Private Sub TripDesc_Loaded( _
      ByVal sender As Object, _
      ByVal e As System.Windows.RoutedEventArgs) _
      Handles Me.Loaded
      Dim TripInformation = XDocument.Load("TripInfo.xml")
      TripName.Text = TripInformation...<TripName>.Value
      TripDesc.Text = TripInformation...<TripDesc>.Value
      TripDate.Text = TripInformation...<TripStartDate>.Value
   End Sub
   Private Sub TripName_LostFocus( _
      ByVal sender As Object, _
      ByVal e As System.Windows.RoutedEventArgs) _
      Handles TripName.LostFocus
      MyTrip.TripInfoSave(TripName.Text, "Name")
   End Sub
   Private Sub TripDesc_LostFocus( _
      ByVal sender As Object, _
      ByVal e As System.Windows.RoutedEventArgs) _
      Handles TripDesc.LostFocus
      MyTrip.TripInfoSave(TripDesc.Text, "Desc")
   End Sub
   Private Sub TripDate_LostFocus( _
      ByVal sender As Object, _
      ByVal e As System.Windows.RoutedEventArgs) _
      Handles TripDate.LostFocus
      MyTrip.TripInfoSave(TripDate.Text, "Date")
   End Sub
   Private Sub ExitDesc_Click( _
      ByVal sender As System.Object, _
      ByVal e As System.Windows.RoutedEventArgs) _
      Handles ExitDesc.Click
      Me.Close()
   End Sub
End Class

Public Class TripInfo
   Public Sub TripInfoSave( _
      ByVal TripInfoVal As String, _
      ByVal TripItem As String)
      Dim TripInformation = XDocument.Load("TripInfo.xml")
      Select Case TripItem
         Case "Name"
            TripInformation...<TripName>.Value = TripInfoVal
         Case "Desc"
            TripInformation...<TripDesc>.Value = TripInfoVal
         Case "Date"
            TripInformation...<TripStartDate>.Value = TripInfoVal
      End Select
      TripInformation.Save("TripInfo.xml")
   End Sub
End Class

The AddADay WPF form

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Several new code ideas are used in this function. First, if you recall in the MainWindow explanation, this is instantiated in different places and the Tag property is used to let the code do different things. (Note the If-Then-Else structure in the Me.Loaded event code.)

Another new code element is the use of XML attributes instead of elements. StayAt, Trans, and Events are attributes in the TripDate element below.

<TripDate
   StayAt="Location"
   Trans="Method"
   Events="Event Description">10/18/2008</TripDate>

Accessing XML attributes requires a slightly different syntax where the attribute is preceded by an "@" character:

StayAtDesc.Text = StayAtElement.@<StayAt>

These are called "XML axis properties" and are new to VB.NET 9.0 with Framework 3.5.

A final new code syntax that should be mentioned is the use of an XML literal with embedded Visual Basic code in the statement:

myTripDate(0).Add( _
   <TripDate><%= LastDateString %></TripDate>)

The XML is coded directly to add this element to the XML TripInfo document. LastDateString, however, is a VB.NET variable, not part of XML. So the ...

<%= ... %>

... syntax is used to embed this directly into the XML literal. One of the really interesting properties of this syntax is that it can be nested too. In other words, you can code something like ...

Dim VBVar As String
Dim y As XElement = <MoreXML/>
Dim x As XElement = <theXML>
                  <%= <MoreXML>
                        <%= VBVar %>
                     </MoreXML> _
                  %>
               </theXML>

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Part 6: TripPlanner - A Complete XAML and WPF Application

©2009 About.com, a part of The New York Times Company.

All rights reserved.