This section provides a brief overview of how you can start creating great Windows applications. It isn t intended as a tutorial, but rather a brief guide to the essentials ”just enough to get you started, refresh your memory, or straighten out any particularly confusing points.
You can create many different types of projects in Visual Studio. The most common is a Windows application, which is a standard Windows desktop program.
To create a new Windows application project in Visual Studio .NET, click File New Project, select the Visual Basic Projects project type and choose the Windows Application template. Enter a name and location, then click on OK. This creates one Solution (similar to a VB6 Project Group), containing one Project.
To put together a regular Windows application project, you typically draw controls onto your form(s), change control properties using the Properties window, and add code to stick it all together. A description of all the inherent toolbox controls can be found in Section III of the appendix.
To add a menu, drag and drop the MainMenu control onto your form. Next, simply click and type to create your menus . To create a hot key, use an ampersand before the desired character (that is, using &File will create an entry called File, which can be activated by pressing the Alt key and F). To change the properties of any individual menu item, select it and then edit its details through the Properties window. For example, to add a keyboard shortcut to a menu item, click the menu item, and select a new value for the Shortcut property through the Properties window.
You can add a new file to your project, such as another form or module, by using the Project menu.
If you want to display one form from another in your application, simply create a new instance of that form class and run the .Show method, as shown here:
Dim frmCalc As New Calculator() frmCalc.Show()
To display a form modally, run the .ShowDialog method instead of .Show . The form will be shown on top of all others in your application until either your code, or the user , closes it.
To create an MDI application, set the IsMdiContainer property of your master form to True . Then, to display a child form within your master form, run code similar to the following, setting the MdiParent property to your master form:
' Code behind Form1, our master Form Dim MyForm As New Form2() MyForm.MdiParent = Me MyForm.Show()
To get a direct reference to the currently active MDI child form, reference the ActiveForm property of your master form object, as so:
' Code behind Form1, our master Form Dim MyForm As Form MyForm = Me.ActiveForm MessageBox.Show(MyForm.Text)
You can arrange your child windows by using the LayoutMdi method of your master form. You can set this to cascade your child windows, tile them horizontally or vertically, or arrange them as icons. Here s an example:
' Code behind Form1, our master Form Me.LayoutMdi(MdiLayout.TileHorizontal)
To maximize your form on startup, change its WindowState property to Maximized . To alter its default startup position to, say, center screen, change the StartPosition property.
Errors in .NET can be handled in two ways. Firstly, using an On Error statement, such as On Error Resume Next (which is second nature to many Visual Basic 6 aficionados). However, the new .NET method of handling errors is more structured and consists of Try-Catch-Finally blocks. Here s an example:
Try Process.Start("c:\badfile.txt") Catch ex As Exception MessageBox.Show("Error occured - " & ex.Message) Finally MessageBox.Show("The Finally block is optional. " & _ "It always runs after everything else - " & _ "whether an error occurred or not.") End Try
To run your application in debug mode, select Debug Start or press F5. To stop on a line in debug mode, move to the line and hit F9. Step through the code using F8, or the menu options in the Debug menu. To remove debugging from a line, move to it and press F9 once again.
To change the startup object for your application ”to, say, another form or Sub Main ”right-click on your project in the Solution Explorer and select Properties. Choose the General menu under Common Properties, and select a startup object from the dropdown list.
If you have purchased or downloaded third-party controls, you can use them by right-clicking on your toolbox, selecting Customize Toolbox, clicking on .NET Framework Components, and then browsing for your control. If you have created a Class Library assembly (akin to an ActiveX DLL) or downloaded a third-party .NET DLL assembly, you can reference and use its functionality by clicking on Project Add Reference, and then browsing for the component. You can also add existing class library projects directly to your solution. Look up referencing objects in the help index for more information.
You can change the application name and copyright and versioning information by editing information in the AssemblyInfo.vb XML-structured file.
To change the final name of your assembly, right-click on your project in the Solution Explorer, select Properties, and alter the Assembly Name value. The Root Namespace value refers the namespace under which your application sits. (In a Windows application, this isn t too important, but in a class library this will affect how users declare a class, that is, Dim MyObject As New MyNamespace.MyClass .)
Compiling your application consists of selecting Build from the Build menu. This generates an executable file (with the .exe extension) in your project s Bin folder. This is a unit of deployment for the .NET Framework and is known as your assembly. It will run on any machine containing the .NET Framework.
Compiled executable files produced by Visual Studio actually contain raw MSIL (Microsoft Intermediate Language) and metadata (descriptive information about your code). When code inside your EXE file is run for the first time on a machine, this MSIL code is optimized for the computer, fully compiled, and then locally cached.
Having the MSIL and metadata helps the .NET Framework analyze your application, automatically handling memory and providing all the plumbing options it does. However, this is all automatic, and so it isn t something you must know about before building .NET applications.
The default configuration for any solution is Debug mode. This creates debug files when you compile your application. Before releasing your final version, change the configuration, through the drop-down list on the standard toolbar, from Debug to Release. This removes all the debug symbols, stops VS .NET producing the PDB debug file in your Bin folder, and further optimizes your code.
Third-party assemblies (such as controls and DLLs) used in your application are typically placed in the Bin folder when you compile. The exception to this are strong-named assemblies, which are accessible by all applications and registered in a global Windows assembly registry called the global assembly cache (GAC). For example, all of the .NET Framework base classes are strong named and registered in the GAC. If you use third-party strong named assemblies, create a setup project to distribute your application (this should automatically distribute and register the assembly in the user s GAC for you), but, if you don t, simply copy the files from your Bin folder to get your application running on another machine.
All of the Toolbox controls and other base Framework classes are included by default in every installation of the .NET Framework. You don t need to distribute any extra DLLs to use them in your application.
For information on creating a setup project and installing the .NET Framework on client machines, check out the Microsoft guide at http://msdn.microsoft.com/library/en-us/dnnetdep/html/dotnetframedepguid.asp.
To change the default application icon, right-click on your project in the Solution Explorer and select Properties. Choose the Build menu under Common Properties, and then choose an icon. This will be the icon shown when your executable is viewed through the Windows Explorer.