Translate the Controller for Windows Mobile Devices
With the desktop component done, now you'll use Visual Studio 2005 to create a Windows Mobile application. Name the project
RemoteController_WM.
 | |
Figure 3. The Default Form1: Populated with the various controls. |
Populate the default Form1 with the following controls (see Figure 3):
Switch to the code-behind of Form1 and declare the following member variable:
Public Class Form1
Private WithEvents serialPort As New IO.Ports.SerialPort
Double-click the
Open MenuItem control and use the code in
Listing 5.
Author's Note: The COM6 is dependent on which COM port you have selected to use on your Windows Mobile device. Refer to the last section of this article to determine which COM port to use. |
Double-click on each of the Button controls and use the code in Listing 6.
Define the DataReceived event so that you can receive messages sent from the desktop application:
'---receives data from the Bluetooth comms---
Private Sub DataReceived( _
ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles serialPort.DataReceived
lblSlideNo.BeginInvoke(New _
myDelegate(AddressOf UpdateLabel), _
New Object() {})
End Sub
The
delegate and
UpdateLabel() subroutines are defined as follows:
'---delegate and subroutine to update the slide no.---
Public Delegate Sub myDelegate()
Public Sub UpdateLabel()
Dim response As String = serialPort.ReadLine()
lblSlideNo.Text = response
End Sub
Finally, service the
Form1_Disposed event to close the serial port when the application exits:
'---when the app exits, close the serial port---
Private Sub Form1_Disposed( _
ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Disposed
serialPort.Close()
End Sub