Rapid Application Development with VB.NET 2.0
Pages: 1, 2
The My.User object contains a great deal of information about the current user.
The IsInRole method is a quick and easy way to determine what permissions to
provide the user.
The CheckKeys method sets each of the key-related check boxes by using the Boolean
parameters of the Keyboard object retrieved through My.Computer:
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
The FillFromClipboard method calls GetText on the Clipboard object, also available
from the 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
Finally, the FillComputerInfo method uses the My.Computer.Info object to obtain
information about memory, the operating system, and the current user, and this
information is added to the lbComputerInfo list box:
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 CheckKeys and FillFromClipBoard
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 GetValue method
of 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
is empty.
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
Setting the Registry uses the SetValue method 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
from My.Computer.
My.Computer.Audio.Play(lbClips.SelectedItem)
Using MyEvents.vb
Finally (and this is about the only tricky bit in this entire example), you
want the ConnectedToNetwork check box to be updated if you unplug your network
cable. To accomplish this, you need to implement a handler for the NetworkAvailabilityChanged
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.

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.

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
box.
Summary
The My object has made creating this application almost absurdly easy. Each
of the 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 My
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
-
Issues with Visual Studio 2005
2008-03-19 12:33:14 Mitomste [View]
-
Details information related to the Memory
2006-10-06 13:30:00 TinaBhate [View]
-
Details information related to the Memory
2006-10-07 08:24:34 JesseLiberty [View]
-
Very Useful Article !!!
2006-08-22 05:49:30 Raja_Krish [View]
- Trackback from http://blogs.developpeur.org/redo/archive/2004/08/17/2302.aspx
Rapid Application Development with VB.NET 2.0
2004-08-17 12:33:10 [View]
- Trackback from http://blogs.msdn.com/vbteam/archive/2004/08/17/215772.aspx
New article on My
2004-08-17 09:33:10 [View]
- Trackback from http://arachnid/Blogs/ewells/archive/0001/01/01/456.aspx
Exploring and Extending the VB.NET 2005 My feature
2004-08-11 23:53:28 [View]
- Trackback from http://arachnid/Blogs/ewells/archive/2004/08/12/456.aspx
Exploring and Extending the VB.NET 2005 My feature
2004-08-11 23:51:59 [View]
- Trackback from http://www.panopticoncentral.net/archive/2004/08/09/1553.aspx
Nice words for My...
2004-08-09 23:26:04 [View]
- Trackback from http://weblogs.asp.net/duncanma/archive/2004/08/05/208646.aspx
Jesse Liberty, author of one of my favorite books (Clouds to Code) gives an overview of
2004-08-05 00:30:39 [View]
-
My* classes in C#
2004-08-02 19:07:38 bendono [View]

