oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Rapid Application Development with VB.NET 2.0

by Jesse Liberty

For a couple of years now, I've been touting the Microsoft-endorsed sentiment that it really doesn't matter if you program in C# or in VB.NET, since both are just syntactic sugar layered on top of MSIL (Microsoft Intermediate Language, the true language of .NET). That appears to be changing a bit with Whidbey.

Microsoft seems to be targeting Visual Basic 2.0 a bit more towards Rapid Application Development (RAD), and thus the designers of VB2 have added a few features to make developing applications quicker and easier. A key feature in this approach is the My object. The My object exposes six top-level objects for fast access to various aspects of your application, environment and resources. These are:

  • My.Application
  • My.Computer
  • My.User
  • My.Webservices
  • My.DataSources

Each of these objects provides extensive properties for access to otherwise difficult-to-find or difficult-to-manipulate aspects of your running application and its environment. To see the My object at work, and to demonstrate how quickly it allows you to put together an application, I've created a small test app, shown in Figure 1.

Figure 1.

Notice that the application indicates whether or not you are currently connected to the network (this is updated by an event if you disconnect or reconnect), whether or not you are an administrator on this machine, which special keys are depressed, and what is currently on the clipboard. The Look Again button reexamines the keys and the clipboard (there is no event handling for when keys are depressed in this implementation). There are also buttons to get or set a specific registry entry, and there is a list box with information about the computer and the current user. Finally, there is a list of .wav files from the Windows\Media directory, and a button that allows you to play each of these sounds (double-clicking on an entry will play it, as well).

Creating this application took about an hour. It would have gone faster, but I was learning more about the My object as I went. By any standard, that is fairly rapid application development.

You can download the code for this application, but it is quite easy to create it for yourself. (Note: the code was created with the Beta 1 version of Visual Studio .NET 2005.)

To create this application yourself, begin by creating a new VB Windows Application. Drag the list boxes, check boxes, and so forth onto the form as shown in Figure 1. For convenience, name them as shown in Figure 2.

Figure 2.

Loading the Form

When the form loads, you'll set the current working directory to C:\Windows\Media and you'll set the data source for lbClips to the list of files in that directory that end in .wav. You accomplish all of this with two lines of code added to the FrmMy_Load event handler:

Private Sub FrmMy_Load(ByVal sender As Object, _
                       ByVal e As System.EventArgs) _
                       Handles Me.Load

The first line of code will set the current directory, using My.Computer.FileSystem.CurrentDirectory.

My.Computer.FileSystem.CurrentDirectory = "C:\Windows\Media"

Let's examine this line a bit more closely. The My object exposes the Computer object, which provides properties for manipulating the components of the computer, including the clock, keyboard, file system, and audio. We'll come back to this object a lot. My.Computer has a property, FileSystem, that returns the My.Computer.FileSystem object, which in turn has properties and methods for working with drives and files. One such property, CurrentDirectory, sets or gets the current directory for the application.

The second line of code in the FrmMy_Load method sets the data source for the list box to the list of files in the current directory.

lbClips.DataSource = _
    My.Computer.FileSystem.GetFiles( _
      My.Computer.FileSystem.CurrentDirectory, _
      False, _

Here, the My.Computer.FileSystem.GetFiles method is being called. It takes three arguments (in this overloaded version): a directory, a Boolean indicating whether to recurse into the subdirectories, and a string indicating the wildcards to use in retrieving the files.

Hey! Presto! the list box is loaded with all of the .wav files from the Windows/Media directory.

The load event handler then calls five other methods as follows:

  • IsNetworked, to test whether you are currently attached to a network.
  • IsAdministrator, to test whether the current user is in the Administrators group.
  • CheckKeys, to set the check boxes for which special keys are depressed (and to try to cheer them up!).
  • FillFromClipBoard, to fill the text box with the current contents of the clipboard.
  • FillComputerInfo, to fill the list box with information about the current computer and user.

Each of these methods is made absurdly simple by the My object.

Public Sub IsNetworked()
    Me.cbNetworked.Checked = My.Computer.Network.IsAvailable
End Sub

The Network object, available as a property of My.Computer, has a property named IsAvailable that returns true if the computer is currently connected to the network. That Boolean value is used to set the Checked property of the cbNetworked check box. Later in this article, I'll show you how to update that check box by responding to the NetworkAvailabilityChanged event.

The IsAdministrator method is equally straightforward.

Private Sub IsAdministrator()
    Me.cbAdministrator.Checked = _
End Sub

Pages: 1, 2

Next Pagearrow