In Visual Basic, the My keyword provides access to a host of useful language features. In this article, Peter Aitken continues his exploration of My.
In an earlier article, we saw how to use My.Settings for an easy way to store and retrieve application settings. The My keyword has a lot more behind it, and any VB programmer needs to know about these features. My is available in any VB application without any effort on the programmer's part.
Note: It's important to distinguish the My keyword from the Me keyword, which is the keyword for accessing the current object in a member function.
ADVERTISEMENT
My.Application
The My.Application object provides properties, events, and methods that are related to the current application. The members that are available depend on the type of application (for example, Window form or Console). Among the ones that I find most useful are the following:
OpenForms property: Returns a collection containing all of the app's open forms.
Info property: Gets the application's Info object which contains information about the application's assembly.
CommandLineArgs property: For console apps, gets any command line arguments that were passed to the application.
SplashScreen property: Gets or sets the Windows form that is the application's splash screen (displayed at startup).
DoEvents method: Permits the operating system to process all messages in the message queue.
GetEnvironmentVariable method: Returns the value of a specified environment variable.
UnhandledException event: Fires when an unhandled exception occurs.
My.Computer
The My.Computer object provides information about the system that the app is running on. Because the app is likely to run on a variety of configurations, most or all of which will be different from the development system, this object is essential for many program actions that involve the system hardware. Among the things you can use My.Computer for are:
Work with the clipboard.
Determine if the system is connected to a network.
Upload and download files.
Work with audio.
Create registry keys.
Manipulate directories and files.
Get information about the keyboard and mouse.
Access the system clock.
Here's an example that uses My.Computer.FileSystem to get the names of all the files with a .vb extension in MyDocuments, including subdirectories, and display them in a TextBox:
For Each s As String In My.Computer.FileSystem.GetFiles _
(My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
FileIO.SearchOption.SearchAllSubDirectories, "*.vb")
TextBox1.AppendText(s & vbCrLf)
Next
See the sections on My.Resources and My.Log (below) for more examples of using My.Computer.
My.Forms
This object provides an instance of each form in the project. Unlike My.Application.OpenForms, it provides access to all forms and not just the open ones. Among the things you can do with My.Forms is to access one form from another and communicate between forms. Sometimes you will use My.Forms without knowing it. For example, suppose code in Form1 manipulates Form2 like this:
Form2.Text = "Hello World."
Form2.Show()
The My.Forms object is implicit, and it would be equivalent to write the following:
The My.Log object provides methods for writing event and exception information to an ASP.NET application's log listeners. It is related to the My.Application.Log object, the difference being that you use My.Application.Log for client applications and My.Log for Web applications.
Here's code that logs the time when the application starts and when it finishes. Put this code in the Startup event procedure:
My.Log.WriteEntry("Application started at " & _
My.Computer.Clock.GmtTime.ToString)
And put this code in the Shutdown event procedure:
My.Log.WriteEntry("Application terminated at " & _
My.Computer.Clock.GmtTime.ToString)