VB.NET Mobile – Hello World!
Hello all – welcome to the first installment of my VB.NET Mobile Blog…the blog that focuses totally on what VB.NET can do on Windows Mobile Devices. Since it’s a developer blog we’ll start out with the ubiquitous “Hello World” but we’re going to do it with a Windows Mobile twist.
It would be fairly pointless for me to do a simple “Hello World” with a button click that pops up a message box or populates a text box with “Hello World” for VB.NET in Windows Mobile since the same code works in VB.NET on the full framework. So rather than do that we’re going to send ourselves an SMS Message that says “Hello World” – what more fitting way to get right into Windows Mobile specific programming than that since SMS seems to be everywhere.
Before we dive into things any further – a couple of notes about the development environment setup you’ll need…if you’re confident you’re already set up and ready to go, humor me and re-read this section anyway, ok?
First off you’re going to need either Visual Studio 2005 Professional (or higher) or Visual Studio 2008 Professional (or higher). Compact Framework isn’t supported in the Visual Studio Express versions…sorry. If you don’t happen to have either of those, or an MSDN Subscription why not check out your local user groups, and Microsoft-centric developer community to see if there are either upcoming Code Camps or User Group meetings. In events I’ve attended in the past I’ve been fortunate enough to give away Not-For-Resale editions of Visual Studio Professional as prizes at those events. You may also find a “Visual Studio Install Fest” going on in your area (or an area worth driving to) where a limited number of users can receive free NFR copies of Visual Studio just for showing up! Just another reason to get involved and stay active in your developer community!
Once you’ve installed Visual Studio (professional or higher), you’re going to need the Windows Mobile 6 SDK’s. You can download both of them using the link below:
One thing worth noting – if you happen to have Visual Studio 2005 with the Windows Mobile SDK’s installed and you later decide to install Visual Studio 2008, read my GeeksWithBlogs entry first – it may save you a bit of a headache.
For the purposes of this article – I’m using Visual Studio 2005…but it should work fine in Visual Studio 2008 as well.
Once you’ve gotten everything set up, launch Visual Studio and create a new Device Application. This will be found under Smart Device->Windows Mobile 6 Professional as shown in the screen shot below:
When you’re done with that, go ahead and make your device application form look similar to example I’ve created below (I’ll provide a download of the solution later!)
As for the code to wire-up this form, it’s very simple as you’ll see. To send an SMS Message we’ll first need to make reference to the Microsoft.WindowsMobile.PocketOutlook namespace. After that it’s as simple as creating a new instance of the SMSMessage class with an overloaded constructor passing in the Recipient phone number and Message text, then invoking the Send method…takes a look at the code below:
Imports Microsoft.WindowsMobile.PocketOutlook
Public Class Form1
Private Sub btnSMSSend_Click_1(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSMSSend.Click
Dim VBMobileSMS As New SmsMessage(Me.txtSMSTo.Text, Me.txtSMSBody.Text)
VBMobileSMS.Send()
End Sub
End Class
In order to make use of this example we’re going to either need a Windows Mobile 6 device to deploy it to (and I realize not everyone has one yet) or we’re going to need the emulator set up with one of the tools…in either case it’s a handy thing to know how to use the tools included with the SDK so we’re going to focus on that now.
You’ll need to launch the Cellular Emulator tool which should be found under Start->Program Files->Windows Mobile 6 SDK->Tools->Cellular Emulator (if you did a standard install). Go ahead and launch that tool. It should look like this when it starts up:
In the bottom left status bar on that window you should see a COM port number – you’ll need to make note of that so you can configure the Windows Mobile device emulator to “talk” to this emulator. In this case we see COM4 is being used. Click the CONFIGURATION Tab and then click the RESET button before launching the Vis
Go launch Visual Studio and open up your project again and do a test deployment to the emulator (I’ve picked Windows Mobile Professional Emulator) and you’ll need to configure this emulator to work with the Cellular Emulator. To do this click File->Configure and on the Emulator Properties window that shows, click on the Configure tab and enter your COM port info in Serial Port 0 as shown below:
Once you’ve configured it all properly you should see the emulator with an “active” connection as shown below:
If all is configured properly then let’s do a build on the project and deploy it to the emulator to test it. You should see a result like the screen below when it’s first run in the emulator:
Once you click the SMS Send button you should see a screen similar to the one below indicating you’ve received a new SMS Message:
If you look over to the Cellular Emulator window on the SMS Tab you should see a record of your message passing through the emulator as shown below:
From that window you can also type a message in the “Send to device” window and click Send to send a message straight to your Windows Mobile 6 emulator from the Cellular Emulator.
And there you have it – a not-so-typical “Hello World” in VB.NET for Windows Mobile Devices using something very specific to Windows Mobile!