Wiring Up all the Event Handlers
With the helper functions defined, it's time to wire up all the event handlers for the various controls.
First, code the Form1_Load event handler, as shown in Listing 4.
When the form is first loaded, you have to create a root node for the TreeView control and load all existing feeds. All subscribed feeds are saved in a plain text file, with the following format:
Feed URL|Feed URL|Feed URL|
An example would be:
http://news.google.com/?output=rss|http://rss.cnn.com/rss/cnn_topstories.rss|
In the
Subscribe MenuItem control's
Click event handler, prompt the user to input a feed's URL and then subscribe to the feed. If the subscription is successful, save the feed URL to file:
'---Subscribe MenuItem control---
Private Sub mnuSubscribe_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles mnuSubscribe.Click
Dim url As String = InputBox( _
"Please enter the feed URL", "Feed URL", "http://")
If url <> String.Empty Then
'---if feed is subscribed successfully---
If SubscribeFeed(url) Then
'---save in feed list---
Dim textwriter As TextWriter
textwriter = File.AppendText(FEEDS_LIST)
textwriter.Write(url & "|")
textwriter.Close()
Else
MsgBox("Feed not subscribed. " & _
"Please check that you have entered " & _
"the correct URL and that you have " & _
"Internet access.")
End If
End If
End Sub
Whenever a node in the
TreeView control is selected, perform a check to see if it is a posting node and enable/disable the
MenuItem controls appropriately (see also
Figure 6).
'---fired when each node is selected---
Private Sub TreeView1_AfterSelect( _
ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.TreeViewEventArgs) _
Handles TreeView1.AfterSelect
If e.Node.ImageIndex <> ICO_POST And _
e.Node.Parent IsNot Nothing Then
mnuUnsubscribe.Enabled = True
mnuRefreshFeed.Enabled = True
Else
mnuUnsubscribe.Enabled = False
mnuRefreshFeed.Enabled = False
End If
End Sub

Figure 6. Everything's Working: Enabling/disabling the MenuItem controls. | |  Figure 7. Viewing the Post: Using the WebBrowser control in Form2. |
When the user selects a post using the Select button on the navigation pad, Form2 containing the WebBrowser control will be loaded and its content set accordingly (see Figure 7). This is handled by the KeyDown event handler:
'---when the user presses the Select key on the keypad---
Private Sub TreeView1_KeyDown( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles TreeView1.KeyDown
Dim node As TreeNode = TreeView1.SelectedNode
If (e.KeyCode = System.Windows.Forms.Keys.Enter) Then
'---Enter key was pressed---
If node.ImageIndex = ICO_POST Then
Dim frm2 As New Form2
With frm2
.Text = node.Text
.WebBrowser1.DocumentText = node.Tag
.Show()
End With
End If
End If
End Sub
To unsubscribe a feed, you will need to remove the feed's URL from the text file and then remove the feed node from the
TreeView control. This is handled by the
Unsubscribe MenuItem control, shown in
Listing 5.
When the user needs to refresh a feed, first make a backup copy of the feed XML document and proceed to subscribe to the same feed again. If the subscription is successful, remove the node containing the old feed. If the subscription is not successful (such as device is disconnected from the Internet, etc), restore the backup feed XML document. This is handled by the Refresh Feed MenuItem control, shown in Listing 6.
Finally, in the Back MenuItem control in Form2, code its Click event handler as follows:
Private Sub mnuBack_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles mnuBack.Click
Me.Hide()
End Sub
That’s it! Press F5 to test the application. If you are testing the application using an emulator, you need to cradle the emulator to ActiveSync first so that you have Internet connectivity on the emulator. To cradle the emulator to
ActiveSync, select Tools | Device Emulator Manager… in Visual Studio 2005 and right-click on the emulator you want to test and select
Cradle.
Windows Mobile 6 Basics
Now you know how to:
- Use the TreeView control to display information hierarchically
- Manipulate files and read and write to text files
- Download files using the WebRequest and WebResponse classes
- Manipulate XML files using XPath expressions