By Tim Patrick, John Clark Craig
Book Price: $49.99 USD
£35.50 GBP
PDF Price: $39.99
Cover | Table of Contents
Module Module1 Sub Main( ) End Sub End Module
Module ConvertTemperature
Sub Main routine in the Module1 module. But since you changed the name, there is no longer a Module1 for Visual Studio to use.
http://www.gotdotnet.com, in its "Workspaces" area.<?xml version="1.0" encoding="UTF-8"?> <CodeSnippets xmlns= "http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title>Convert a Number to a Hexadecimal String</Title> <Author>Microsoft Corporation</Author> <Description>Returns the hexadecimal representation of an integer.</Description> <Shortcut>typeHex</Shortcut> </Header> <Snippet> <Imports> <Import> <Namespace>System</Namespace> </Import> <Import> <Namespace>Microsoft.VisualBasic</Namespace> </Import> </Imports> <Declarations> <Literal> <ID>Number</ID> <Type /> <ToolTip>Replace with an integer.</ToolTip> <Default>48</Default> <Function /> </Literal> </Declarations> <Code Language="VB" Kind="method body"> <![CDATA[Dim hexString As String = Hex($Number$)]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets>
My.Application.Info.Version object to access the version number of the application, and store the result in a Label control.1.2.3.4
System.Version class obtained from the My.Application.Info.Version object. You can use the members of this class to display version information when needed. The following code assumes your form has a label named VersionNumber:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
With My.Application.Info.Version
VersionNumber.Text = "Version " & .Major & _
"." & .Minor & " (Build " & .Build & "." & _
.Revision & ")"
End With
End Sub
End Class
1.2.3.4.
VersionNumber.Text = My.Application.Info.Version.ToString()
<Assembly: AssemblyVersion("1.0.0.0")>
<Assembly: AssemblyVersion("1.2.*")>
<Assembly: AssemblyVersion("1.2.3.*")>
Main(), which will appear somewhere in your application's source code. You can use the "Startup form" field on the Application tab to tell Visual Basic to use the Main() method included with a specific form's code. If you do not supply such a method, Visual Basic will implicitly add one to the startup form, using code that looks something like this:Public Shared Sub Main() Application.Run(My.Forms.Form1) End Sub
Main() method to a module in your application, and use that as the startup code. You will need to display forms on your own.Main() method with at least the following code:Module Module1 Public Sub Main() ' ----- Add startup code here, then… Application.Run(My.Forms.Form1) ' …passing the startup form as the argument. End Sub End Module
Main() method as the startup code for your application, via the Application tab of the Project Properties window. Disable the Windows Forms Application Framework by clearing the "Enable application framework" field. Then set the "Startup form" field on that same tab to "Sub Main."Main(). You can supply your own Main() method, and it doesn't need to be part of a form. Adding it to a module with your own initialization code gives you the most control over the application's startup process.
Application.Run() method runs the primary message loop for your application, a standard part of all Windows desktop programs. Pass an instance of your startup form as an argument; Visual Basic will display this form and keep the program running until the user closes this form.Main() method, some of the convenience and usability features included with the Framework will not be enabled by default. For instance, you will have to manually display and hide any "splash" form that appears during the initialization phase of your application.Command() function, part of the Microsoft.VisualBasic namespace. This function returns the entire set of command-line options as a String. For instance, if the user enters the following command:MyApp.exe /option1 /option2 filename.txt
Command( ) function returns:/option1 /option2 filename.txt
Command( ) returns only the options, not the program name.Command() returns a single string with the entire command-line option text, the responsibility for parsing each option from the string rests on your shoulders. However, Visual Basic also supplies a pre-parsed version of the options through the
My.Application.CommandLineArgs collection. Each zero-based argument in the collection includes one of the original space-delimited options as entered by the user. Thus, using the example command line from just a few paragraphs ago, the following method call:MsgBox(My.Application.CommandLineArgs(1))
option2, because the collection is zero-based.Notepad.exe c:\temp\DataFile.txt
Click event handler for the label that just displays a message box. We used Microsoft's Stop statement on errors when in the debugging environment but log the errors to a file when running as a standalone application.System.Diagnostics.Debugger.
IsAttached flag. If this property is True, your application is running in the development environment.IsAttached property indicates True whenever your application is running in a debugger that properly sets the underlying value of this
flag. That means that if the flag is True, the program may be running in some environment other than Visual Studio. But if your program is running in some nonVisual Studio debugger, there are probably bigger issues of concern.My.Application.
GetEnvironmentVariable() method to retrieve specific environment variable values.PATH variable stored a list of directories Windows used to locate programs. Other applications could read the PATH variable for their own use.
PATH environment variable from Visual Basic, use this statement:
Dim thePath As String = _
My.Application.GetEnvironmentVariable("PATH")
Environ() function that provides similar functionality:
Dim thePath As String = Environ("PATH")
Environ() returns an empty string without raising an error.Environ() also retrieves environment variables by numeric position. The following code scans through the set of environment variables until it hits a blank result, indicating the end of the set of variables:Dim counter As Integer Dim fullVariable As String Dim namePart As String Dim valuePart As String Dim equalsPosition As Integer For counter = 1 To 255 fullVariable = Environ(counter) If (fullVariable = "") Then Exit For equalsPosition = InStr(fullVariable, "=") If (equalsPosition > 0) Then namePart = Left(fullVariable, equalsPosition - 1) valuePart = Mid(fullVariable, equalsPosition + 1) ' ----- Use these values as needed. End If Next counter
My.Computer.
Registry object to read, write, and otherwise manipulate registry information.My.Computer.Registry object includes the following members:ClassesRoot
field
RegistryKey object that refers to the HKEY_CLASSES_ROOT top-level key of the registry.CurrentConfig
field
RegistryKey object that refers to the HKEY_CURRENT_CONFIG top-level key of the registry.CurrentUser
field
RegistryKey object that refers to the HKEY_CURRENT_USER top-level key of the registry.DynData
field
RegistryKey object that refers to the HKEY_DYN_DATA top-level key of the registry.GetValue( )
method
LocalMachine
field
RegistryKey object that refers to the HKEY_LOCAL_MACHINE top-level key of the registry.PerformanceData
field
RegistryKey object that refers to the HKEY_PERFORMANCE_DATA top-level key of the registry.SetValue( )
method
Users
field
RegistryKey object that refers to the HKEY_USERS top-level key of the registry.Registry members return a RegistryKey object, a generic object that can refer to any key within the registry. This object also has many useful members. Some members let you manipulate the keys that appear just below the one represented by the My namespace. Microsoft introduced this new feature in the 2005 release of the language.My namespace was added to Visual Basic to help restore some of the simplicity and accessibility of Visual Basic that was lost in its transition to .NET.Dim installedMemory As Long = _ My.Computer.Info.TotalPhysicalMemory
System.Windows.Forms.
SystemInformation object, which has dozens of informative members.My hierarchy makes an incredible number of features available in one easy-to-access place. Table 2-1 includes a small sampling of the information you can obtain from the My namespace.|
If you need to access this information…
|
…use this
My namespace member
|
|---|---|
|
The command-line arguments used to start the program
|
My.Application.CommandLineArgs
|
|
The application version number
|
My.Application.Info.Version
|
|
The set of all forms currently open
|
My.Application.OpenForms
|
|
Features to read and write clipboard data |
My.User.Name property to get the domain and login ID of the current user.My.User.Name property returns a string containing the current user ID and related domain name in the format "domain/user." If the user is part of a workgroup instead of a domain, the domain portion may be replaced by the local machine name. Applications written using ASP.NET do not have access to the same type of user information as desktop applications because Web Forms programs run in the context of a special web-application user.My.User.Name may return information about the current user in a different format.System.Environment.MachineName
System.Environment.UserDomainName
System.Environment.UserName
\\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion
RegisteredOrganization and RegisteredOwner values within that key supply the values that you often see when installing new software on your system.Module key-word instead of Class—to your application.
Module Module1 End Module
Sub procedures, functions, properties, events, delegates, classes, structures, and enumerations. Before coding each member, decide the
access you want to grant and prefix the definition with the appropriate access keyword (
Public,
Shared, or
FriendModule key-word instead of Class—to your application.
Module Module1 End Module
Sub procedures, functions, properties, events, delegates, classes, structures, and enumerations. Before coding each member, decide the
access you want to grant and prefix the definition with the appropriate access keyword (
Public,
Shared, or
Friend). For instance, the following block of code adds a function to the module Module1 and assigns the function