Rapid Application Development with VB.NET 2.0
Pages: 1, 2
My.User object contains a great deal of information about the current user.
IsInRole method is a quick and easy way to determine what permissions to
provide the user.
CheckKeys method sets each of the key-related check boxes by using the Boolean
parameters of the
Keyboard object retrieved through
Private Sub CheckKeys() Me.cbAltKey.Checked = My.Computer.Keyboard.AltKeyDown Me.cbCapsLock.Checked = My.Computer.Keyboard.CapsLock Me.cbCtrlKey.Checked = My.Computer.Keyboard.CtrlKeyDown Me.cbNumLock.Checked = My.Computer.Keyboard.NumLock Me.cbScrollLock.Checked = My.Computer.Keyboard.ScrollLock Me.cbShiftKey.Checked = My.Computer.Keyboard.ShiftKeyDown End Sub
FillFromClipboard method calls
GetText on the
Clipboard object, also available
My.Computer object. The
Clipboard object can be used to place items
onto the clipboard as well, including images and other non-text objects:
Private Sub FillFromClipBoard() Me.txtClipBoard.Text = My.Computer.Clipboard.GetText() End Sub
FillComputerInfomethod uses the
My.Computer.Infoobject to obtain information about memory, the operating system, and the current user, and this information is added to the
Private Sub FillComptuerInfo() Me.lbComputerInfo.Items.Add( "Total memory: " + _ My.Computer.Info.TotalPhysicalMemory.ToString()) Me.lbComputerInfo.Items.Add("Physical memory: " + _ My.Computer.Info.AvailablePhysicalMemory.ToString()) Me.lbComputerInfo.Items.Add("Virtual memory: " + _ My.Computer.Info.AvailableVirtualMemory.ToString()) Me.lbComputerInfo.Items.Add("Machine Name: " + _ My.Computer.Info.MachineName.ToString()) Me.lbComputerInfo.Items.Add("User: " + _ My.Computer.Info.UserName.ToString()) Me.lbComputerInfo.Items.Add("O.S.: " + _ My.Computer.Info.OSFullName.ToString()) End Sub
The Look Again button event handler just calls
to reset the check boxes and the text based on changes since the form was loaded.
Implementing the Interactions
In the upper-right-hand corer, you see a text box and two buttons: Set and
Get. These are used to write a string to a key in the registry, and to read
that string from the registry, respectively. The Get button's handler calls the
Registry object obtained through
My.Computer. This method takes three
arguments: the key, the value name, and the default value to return if the key
Private Sub btnGetRegistry_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnGetRegistry.Click Me.txtRegistry.Text = My.Computer.Registry.GetValue( _ "HKEY_CURRENT_USER\MyObjectKey", _ "MyObjectValue", _ "No Registry value stored").ToString() End Sub
SetValuemethod on the same object, and the three arguments this time are the key, the name of the value, and the actual text to store.
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\MyObjectKey", _ "MyObjectValue", _ Me.txtRegistry.Text) Me.txtRegistry.Text = String.Empty
The second line of code clears the text box when the text is written to the registry.
An event handler is created for both the
Click event on the Play button and
for a double-click event on the
lbClips list box.
Private Sub Play_Event(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnPlay.Click, lbClips.DoubleClick
The job of this event holder is to find the currently selected .wav file and
to play it. This is done using the
Play method of the
Audio object obtained
Finally (and this is about the only tricky bit in this entire example), you
ConnectedToNetwork check box to be updated if you unplug your network
cable. To accomplish this, you need to implement a handler for the
event. To do so, return to the solution explorer and click the Show All Files
button. It turns out that your application includes a hidden file, named MyEvents.vb,
as shown in Figure 3.
Open that file and click on the drop-down menu to show the events. Click on the
NetworkAvailabilityChanged event and the outline of the event handler is created
for you, as shown in Figure 4.
Within that handler, just add a call to
FrmMy.IsNetworked() so that when the
network availability changes (and the event is fired) you can update your check
My object has made creating this application almost absurdly easy. Each
My object objects has many more properties and methods than are shown here,
but they are easy to explore through Intellisense and the MSDN documentation,
now that you know they exist and have seen how they are used.
VB 2 has taken a dramatic lead in Rapid Application Development with the
object. This raises the question of why this facility is not available in C#.
Unless there is a clear performance penalty in using the
My object (I've not
tested that yet) there is no reason to turn our C# noses up at anything that
makes programming easier; after all, the more that is provided by the framework,
the more we can concentrate on designing and building the application.
The example code for this article is available for download.
Jesse Liberty is a senior program manager for Microsoft Silverlight where he is responsible for the creation of tutorials, videos and other content to facilitate the learning and use of Silverlight. Jesse is well known in the industry in part because of his many bestselling books, including O'Reilly Media's Programming .NET 3.5, Programming C# 3.0, Learning ASP.NET with AJAX and the soon to be published Programming Silverlight.
Return to ONDotnet.com