This chapter describes controls,
Appendix A describes the most useful properties, methods, and events provided by the Control class. All controls that inherit from this class also inherit these properties, methods, and events, unless they take action to override the Control class’s behavior.
Appendix G describes the standard Windows controls in detail. This appendix can help you understand the controls and decide which is best for a particular situation.
This chapter gives some useful background for working with controls in general, but there’s more to building a Visual Basic application than just controls. You also need to understand the code behind the controls that lets the program take values from controls, manipulate those values, and display a result in the controls. The
A Visual Basic solution contains one or more
This chapter describes the basic structure of a Visual Basic project. It explains the functions of some of the most common files and
This chapter also explains the basic structure of source code files. It explains
Figure 3-1 shows the Solution Explorer window for a solution that contains two projects. The solution named MySolution contains two projects named WindowsApplication1 and WindowsApplication2 . Each project contains a My Project item that represents the project’s properties, various files containing project configuration settings, and a form named Form1 .
Figure 3-1: A solution contains one or more projects that contain files.
In WindowsApplication2 , the Show Hidden Files button has been clicked (the second button from the left with the box around it) so that you can see all the project’s files. WindowsApplication1 has similar files, but they are hidden by default.
These files are generated by Visual Basic for various purposes. For example, Resources.resx contains resources used by the project and Settings.settings contains project settings.
Settings are values that control the execution of the application. These might include flags telling the program what options to display or how to perform certain
The following list describes the files contained in WindowsApplication2 and shown in Figure 3-1. The types of files generated for a project changed a bit between some of the early versions of Visual Studio 2005, so the exact files you see may be different from those shown here. This list should give you an idea of what’s involved in building a project, however.
WindowsApplication2 - This folder represents the entire project. You can expand or collapse it to show and hide the project’s details.
My Project - This folder represents the project’s assembly information, application-level events, resources, and configuration settings. Double-click the My Project entry to view and edit these values.
Application.myapp - This XML file defines application properties (such as whether it’s a single instance program and whether its shutdown mode is AfterMainFormCloses or AfterAllFormsClose ).
Application.Designer.vb - This file contains code that works with the values defined in Application.myapp .
- This file contains information about the application’s assembly such as copyright information, company
Resources.resx - This resource file contains project’s resources.
Resources.Designer.vb - This file contains Visual Basic code for manipulating resources defined in Resources.resx . For example, if you define a string resource named Greeting in Resources.resx , Visual Basic adds a read-only property to this module so you can read the value of Greeting as shown in the following code.
Settings.settings - This file contains settings that you can define to control the application.
Settings.Designer.vb - This file contains Visual Basic code for manipulating settings defined in Settings.settings , much as Resources.Designer.vb contains code for working with Resources.resx . For example, the following code uses the UserMode setting.
If My.Settings.UserMode = "Clerk" Then ...
- This folder lists references to external components such as DLLs and COM
bin - This folder is used to build the application before it is executed. It contains the compiled .exe file.
obj - This folder is used to build the application before it is executed.
ApplicationEvents.vb - This code file contains application-level event handlers for the MyApplication object. For example, it contains the application’s Startup , Shutdown , and NetworkAvailabilityChanged event handlers.
Form1.vb - This is a form file. It contains the code you write for the form, its controls, their event handlers, and so forth.
- This file contains designer-generated Visual Basic code that builds the form. It initializes the form when it is created, adds the controls you placed on the form, and defines
Some projects may have other hidden files. For example, when you add controls to a form, the designer adds a resource file to the form to hold any resources needed by the controls.
Normally, you do not need to work directly with the hidden files. You can use other tools to modify them indirectly instead. For example, the files Resources.Designer.vb , Settings.Designer.vb , and Form1.Designer.vb are automatically generated when you modify their corresponding source files Resources.resx , Settings.settings , and Form1.vb .
You don’t even need to work with all of these source files directly. For example, if you double-click the My Project item in Solution Explorer, the property pages shown in Figure 3-2 appear. The Application tab shown in this figure lets you set high-level application settings. The View Application Events button at the bottom of the figure lets you edit the application-level events stored in ApplicationEvents.vb .
Figure 3-2: These property pages let you define project’s resources, settings, and general configuration.
The Resources tab shown in Figure 3-2 lets you view, add, and remove project references. As you can probably guess, the Resources and Settings tabs let you edit the project’s resources and settings.
A particularly important section hidden away in these tabs is the assembly information. When you click the Assembly Information button shown in Figure 3-2, the dialog box shown in Figure 3-3 appears.
Figure 3-3: The Assembly Information dialog box lets you define basic project information such astitle, copyright, and version number.
Many of the items in this dialog box, such as the application’s title and description, are self-explanatory. They are simply strings that the assembly carries around for identification. The assembly and file versions are used by the Visual Studio runtime to verify compatibility between an application’s components. The GUID (globally unique identifier) uniquely identifies the assembly and is generated by Visual Studio. The Make assembly COM-Visible check box lets you determine whether the assembly should make types defined in the assembly visible to COM applications. For more information on this dialog box, see msdn2.microsoft.com/en-gb/library/1h52t681(VS.80).aspx .
An assembly is the fundamental unit of deployment and version control in Visual Studio .NET. An assembly can contain an executable application, a DLL, or control library. Usually a project is contained in a single assembly.
The Assembly Information dialog box lets you define information that should be associated with the assembly, including the assembly’s company name, description, copyright, trademark, name, product name, title, and version (which includes major, minor, revision, and build values).
The My.Application.AssemblyInfo namespace provides easy access to these values at runtime. The following code shows how a program can display this information in a series of labels when it starts.
Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load lblCompanyName.Text = My.Application.Info.CompanyName lblDescription.Text = My.Application.Info.Description lblCopyright.Text = My.Application.Info.Copyright lblTrademark.Text = My.Application.Info.Trademark lblDirectoryPath.Text = My.Application.Info.DirectoryPath lblProductName.Text = My.Application.Info.ProductName lblTitle.Text = My.Application.Info.Title lblVersion.Text = My.Application.
Info.Version.ToString End Sub