Visual Basic Programming for Windows CE
by Marc Robards
Introduction
The Microsoft Windows CE Toolkit for Visual Basic 6.0 (the
Toolkit for VB 5.0 is severely limited compared to the 6.0
release, upgrade now!) is everything and more you need to
develop applications completely in Visual Basic for almost any
device running Windows CE, including H/PC (running Windows CE
2.0 or later), H/PC Pro (running Windows CE 2.11 or later),
P/PC (running Windows CE 2.11 or later), and even embedded CE
devices. Confidently living up to it's "Toolkit" name, a heap
of tools are built in to make your VB-CE experience more
rewarding, including a slick Application Install Wizard, CE
Spy, a Registry Editor, and Remote Zoom, to name a few. All
this for under $200 ($99.95 for upgrade, free if you have a
Universal Subscription to MSDN) makes it relatively painless
to get started developing the next killer app for CE entirely
in Visual Basic.
Specifics
As long as you're running Windows NT/2000 (sorry, no CE SDK
currently released will install on Win9x) on your development
machine, there's no additional configuration necessary for
installation. The CE Toolkit installation will set everything
up for you, and put all the shortcuts right in your
Start->Programs->Microsoft Visual Studio 6.0 Start Menu.
One thing to watch for is that after the Toolkit installs, the
H/PC SDK will automatically start installing. You can cancel
out of this without doing and damage if you don't plan on
developing for the H/PC platform and want to install only the
SDK for the device you want. They're available for free
download from Microsoft's Windows CE Toolkits page, msdn.microsoft.com/cetools, and by-the-by,
the only CE SDKs currently supported for development under VB
are: H/PC, H/PC Pro, and P/PC. (I know, you were dreaming
about writing a game in VB for the Dreamcast; however, that
one's Visual C++ only.)
There are things you need to know about developing for CE
devices under Visual Basic. The most important difference from
VB desktop development is that there is no VB runtime DLL in
CE development. Great, right!?! Wrong. Instead of eating up
the very limited memory on a CE device with a 1.34MB
msvbvm60.dll, Microsoft uses the VBScript dll to make VB work
under CE. At 417KB, it makes for a much more compact
installation package. It also makes for a bit of creative
coding when it comes to writing CE applications under VB. That
means you can't use error trapping like you're normally used
to in VB (no On Error Goto MyErrorTrap), you don't have
a Dir statement (unless you include the File object reference,
which means yet another dll to install), and no more End
statement to easily quit your app.
However, it's not the end of the world. VBScript is a very
feature-rich language, so you just need to think of new ways
to get the job done. And hey, that's what programming is all
about, right? If you're developing for a CE device, you should
be adopting a minimalist philosophy with your code anyway,
eliminating any unnecessary functionality and keeping your app
a lean, mean, processing machine. Now for the good news: The
CE Toolkit for VB 6.0 does give you a number of standard
controls to work with, either native (Frame, Label, Line,
CommandButton, ListBox, Menu, OptionButton, Scrollbar, Shape,
TextBox, Timer) or ActiveX (CommandBar, File, FileSystem,
Image, PictureBox, Winsock). Also included in the "ActiveX
Control Pack" comes the Grid, TabStrip, TreeView, ListView,
ImageList, and CommonDialog controls. Say goodbye to the
Directory, Drive, and File List Boxes, the Data control, and
the OLE control. Both classes and COM are still around, and
for all you database junkies, there is a version of ADO
available for CE (see our feature on ADO
for Windows CE for more information). While you can
use ActiveX controls in VBCE, if you've got your own ActiveX
Control you want to port to CE, brush up on that C++, because
VBCE currently doesn't support ActiveX control creation.
You'll have to use the Visual C++ Toolkit for that.
Another dramatic difference in VBCE development is the
debugger. VB's standard debugger doesn't work with Windows CE,
so the Toolkit comes with its own. It's actually a completely
separate debugging program that runs from within the VB IDE.
You can set breakpoints, but only in the CE debugger (usual VB
breakpoints don't work), and you can still step through, but
it's a little difficult to use at first. (See Figure 1)
Figure 1 - The Windows CE Toolkit for
Visual Basic Debugger
VBCE Example
To run the example, you'll need to
install the P/PC SDK, and the ADOCE SDK. You'll also need to
copy the vbce.mdb file to the My Documents folder on your
device.
Download
the Example Source Code
For this article, I've written a very simple database
application for the CE environment in VB enlisting the help of
the ADOCE Control. (Control? Well, it's actually a reference,
but Microsoft calls it a control.) I've also chosen the P/PC
platform, which appears to be the new flagship device for CE
based on Microsoft's new Pocket PC initiative. The app
consists of one form (see Figure 2), complete with a
CommandBar (Microsoft recommends you have a command bar on
P/PC applications, H/PC and H/PC Pro need not apply). On the
form are a few Labels and TextBoxes for data viewing, and a
couple CommandButtons for moving from record to record. To
round it out, I've added a CommonDialog Control for example
purposes and to let the Help button on the CommandBar actually
go to a help file.
Figure 2 - the VBCE Example
form
The code is fairly straightforward, but there are a few
things I'd like to point out. The CommandBar is a neat little
control, allowing you to put on menu items, combo boxes, and
buttons for application control. For this example app, I've
only added a File->Exit menu item, to show you how to get
started. I've taken off the OK button, so my program isn't
confused with a dialog box. In the Form_Load event, the menu
items are inserted:
'resize the command bar to fit the form
CommandBar1.Width = Me.Width
'add a menu
Dim M As CommandBarMenuBar
Set M = CommandBar1.Controls.Add(cbrMenuBar)
Dim mnuFile
Set mnuFile = M.Items.Add(, "mnuFile", "File")
mnuFile.SubItems.Add , "mnuExit", "Exit"
The syntax for adding items is
items.Add([index], [key],
[caption], [style]). Make sure you set the
Key property, because without the availabitly of the
typical mnuExit_Click events, you have to trap the
click in the CommandBar_MenuClick event, as so:
If Item.Key = "mnuExit" Then
'clean up
rs.Close
Set rs = Nothing
'App.End is the only way to exit the program programmatically
App.End
End If
Notice the App.End command in the code above. Without
the End command, this is the only way you can exit the
program from code. Here are a few tips to remember when you're
developing for a Windows CE device:
- Set your form's BorderStyle property to 0 - None and
maximize it, which will keep your users from being able to
drag the form all around (and off of) the screen.
- Keep your code tight and your functionality at the
"absolutely necessary" level. Remember, you're developing
for a device with very limited memory!
- A maximized form has dimensions of 240 x 320 on a Palm
PC device, and 640 x 320 on a Handheld PC. Size your forms
accordingly.
- Remember there is no MousePointer in CE.
- Keep data entry to a minimum in your apps. Nobody wants
to type (or jot with a stylus) in a book, that's what
desktop computers are for!
- Put your application name in the Form.Caption property
of your main form. The value is what will show up under CE's
Task Manager.
Figure 3 - The VBCE Example Program in
action
Distributing your VBCE application
The Application
Install Wizard included in the Toolkit is a most helpful
addition. Much less cumbersome than most third-party
installers, the wizard brings to VBCE distribution all the
essential features you've grown to love in the standard Visual
Studio Package and Deployment Wizard. You can select where you
want your application files copied on the device, which
ActiveX controls or additional files you want installed, and
whether or not to copy the VBCE runtime files. For getting
your brand-new VBCE app on the road in a jiffy, the
Application Install Wizard is the way to go.
Other Resources
Books
See our Store section
for recommended Visual Basic and Windows CE books that can be
ordered directly from WirelessDevNet.com.
Links
Microsoft
Windows CE
Microsoft Windows CE
Toolkits
MSDN
Library - Windows CE
VBCE.com
Next: Visual
C++ Programming For Windows
CE