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
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 5 of 5.
-
Details information related to the Memory
2006-10-06 13:30:00 TinaBhate [Reply | View]
How can i get the detailed information related to the memory.Eg. Slot 0 = 512MB,
Slot 1=128MB.
Please let me know.
Email:- tinabhate@hotmail.com
Thanks.
Regards,
Tina -
Details information related to the Memory
2006-10-07 08:24:34 JesseLiberty [Reply | View]
That is a good question, but it goes pretty far beyond the scope of this article. As soon as I can find some time I'll do a bit of research but you may want to post your question on a more general news group. Sorry that i don't have it at my finger tips. Best of luck. -j
-
Very Useful Article !!!
2006-08-22 05:49:30 Raja_Krish [Reply | View]
I wish to thank both of u guys for providing us an insight of how effectively we could use the MY object. Especially the C# version is a new information which I didnt know about and I had been thinking that MY could only be used in VB .Net.
Regards,
Raja
-
My* classes in C#
2004-08-02 19:07:38 bendono [Reply | View]
Thanks for another good article. I enjoy reading them. In this one you discuss the My* classes in the forthcoming VB.Net 2.0. You also ask why "this facility is not available in C#." As a matter of fact, they are. You can use them from any .Net language including non-Microsoft ones as well. Here are instructions to use it from C#:
1. Add a reference to Microsoft.VisualBasic.dll.
Note that this is always references for you in all VB projects which is why you need not reference it in VB.
(Optional)
2. To save yourself a little typing, add "using Microsoft.VisualBasic.MyServices;".
Note that these are the same instructions to use any other .Net DLL.
Here are a few examples from your article and equivalant C#:
'VB code
Me.cbNetworked.Checked = My.Computer.Network.IsAvailable
// C# code
MyComputer mc = new MyComputer();
cbNetworked.Checked = mc.Network.IsAvailable;
'VB code
Me.cbAltKey.Checked = My.Computer.Keyboard.AltKeyDown
Me.cbCapsLock.Checked = My.Computer.Keyboard.CapsLock
Me.cbCtrlKey.Checked = My.Computer.Keyboard.CtrlKeyDown
' etc...
// C# code
MyComputer mc = new MyComputer();
this.cbAltKey.Checked = mc.Computer.Keyboard.AltKeyDown;
this.cbCapsLock.Checked = mc.Computer.Keyboard.CapsLock;
this.cbCtrlKey.Checked = mc.Computer.Keyboard.CtrlKeyDown;
' etc...
'VB code
My.Computer.Audio.Play(lbClips.SelectedItem)
// C# code
MyAudio ma = new MyAudio();
ma.Play(lbClips.SelectedItem);
'VB code
My.Computer.Info.TotalPhysicalMemory
'etc...
// C# code
MyComputer mc = new MyComputer();
mc.Info.TotalPhysicalMemory;
// etc...
The list could go on and on, but I think I've made my point. If you look at the IL for most of these, they are just thin wrappers around other types in the standard framework. The main advantages that I see with the My* classes are these:
1. Simplified syntax. No need to create an instance of said object; can use as if it were static thanks to the compiler (which they really aren't).
2. Quick and easy location for related functionality. Some of the other methods are slightly harder to track down without looking at the IL. I don't think it would be to difficult to quickly create a table mapping each My* to the real .Net type. "Reflector" is a great tool to assist with that.
I don't see a need for a "My" keyword in C#. All of the functionality is already there for comsumption now. A new keyword would just bloat things and not add anything significant. If absolutlely necessary, one could just create a static wrapper class called "My" that would duplicate the same functionality.






This:
lbClips.DataSource = My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.CurrentDirectory, False, "*.wav")
Must be changed to this:
lbClips.DataSource = My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.CurrentDirectory, FileIO.SearchOption.SearchTopLevelOnly, "*.wav")
In addition,
My.Computer.Info.MachineName is underlined with the error 'MachineName' is not a member of 'Microsoft.VisualBasic.Devices.ComputerInfo'.
Are there known issues with using Visual Studio 2005? These seemingly minor issues are annoying.