VB code (i)

Source: Internet
Author: User

Before you start coding

The most important (and often overlooked) part of creating applications in Visual Basic is the design phase. It's obvious that you need to design a user interface for your application, but the structure of the code you need to design is less obvious. Different methods of constructing an application can cause the performance of the application and the maintainability and usability of the code. The code for the Visual Basic application is organized in a layered manner. Typical applications include modules: Form modules for each form in the application, optional standard modules for shared code, and optional class modules. Each module contains a number of procedures that contain code: an event procedure, a sub child procedure, or a Function procedure, and a property procedure. Determine what process belongs to the module, which is related to the type of application you are creating. Because Visual Basic is object-based, it is useful to consider an application with an object represented by the application. In this chapter, the sample application Vcr.vbp is built on the object of a cassette recorder and a TV set. The VCR application contains two form modules, a standard module, and two class modules. Use the Object Browser to check the structure of the project (Figure 5.2).

The main form (FRMVCR) of the VCR application is a visual image of the VCR combined with the TV screen (Figure 5.3). It consists of several objects, which are modeled on the real-world picture. A set of command buttons (Cmdplay, Cmdrecord, and so on) mimic the buttons that manipulate the VCR. The software VCR also includes a clock (lbltime), a channel indicator (LBLCHANNEL), a function indicator (Shpplay, Shprecord, and so on), as well as a CRT (PICTV). The Vcr.frm form module includes an event procedure for all of these objects.

In many cases, there is a duplication of multiple object sharing processes. For example, pressing the like, rewind, or video button requires the pause and stop buttons to be valid. It is a good idea to create a shared child procedure that all buttons can call, rather than repeating this code in the Click event procedure for each button. If you want to modify these subroutines in the future, you can modify them in only one place. The standard module Vcr.bas contains a variety of shared processes.
Some parts of the VCR are not seen, such as the logic of tape conveyors or recorded TV. Similarly, some features of the software VCR do not have visual images. These sections and functions are implemented as two class modules Recorder.cls and Tape.cls. The Clsrecorder module contains code to start the video process, while the Clstape module contains code that controls the direction and speed of the tape. The classes defined in these modules are not directly related to any objects in the form. Because they are separate code modules, you can easily recreate a recording program without making any changes.
In addition to designing the code structure, it is important to establish a naming convention. By default, VisualBasic names the first form of the project as Form1, the second form is Form2, and so on. If you have several forms in your application, it is best to give them meaningful names to avoid confusion when writing or editing your code. Some of the naming conventions recommended by Visual Basic are listed in Appendix B, "Visual Basic coding Conventions."
As you learn more about objects and learn to write code, refer to the VCR sample application to find examples of various coding techniques.

Code writing mechanism
Before you start writing code, it is important to understand the mechanism that Visual Basic writes code. Like any programming language, Visual Basic has its own rules for organizing, editing, and formatting code.

Code module
The code for Visual Basic is stored in the module. There are three types of modules: forms, standards, and classes. A simple application can have only one form, and all the code for the application resides in the form module. And when your application is large and complex, you need to add additional forms. You may end up finding common code to execute in several forms. Because you don't want to repeat code in two forms, you create a stand-alone module that contains the process of implementing common code. The standalone module should be a standard module. You can then create a module library that contains shared procedures.
Each standard module, class module, and form module can contain:
Statement. You can place declarations of constants, types, variables, and dynamic-link library (DLL) procedures at the module level of a form, class, or standard module.
Process. A Sub, Function, or property procedure contains snippets of code that can be executed as a unit. These are discussed in the "Process Overview" section later in this chapter.

form module
Form module (the file name extension is. FRM) is the foundation of most Visual basic applications. Form modules can contain form-level declarations that process events, common procedures, and variables, constants, types, and external procedures. If you want to observe a form module in a text editor, you also see a description of the form and its controls, including their property setting values. The code written to the form module is specific to the particular application that the form belongs to, and it can also refer to other forms or objects within that application.

Standard modules
Standard module (file name extension. BAS) is a container for procedures and declarations that are accessed by other modules within an application. They can contain variables, constants, types, external procedures, and global (valid for the entire application) declarations or module-level declarations. Code written to a standard module does not have to be tied to a particular application, and if you can pay attention to using names to refer to forms and controls, you can reuse standard modules in many different applications.

class module
class modules in Visual Basic (the file name extension is. CLS) is the foundation of object-oriented programming. You can write code in a class module to create a new object. These new objects can include custom properties and methods. In fact, a form is a class of modules on which you can place controls to display a form window.
For more information about writing code in a class module, see Chapter Nineth, "Programming with objects."
Note the Professional and Enterprise editions of Visual Basic also contain ActiveX documents, ActiveX designers, and user controls. They introduce a new module type with different file name extensions. From the point of view of writing code, these modules should be treated as form modules.

Using the Code Editor
The Visual Basic Code Editor is a window with most of the code written on this window. It is like a highly specialized word processing software that has many features that make it easy to write Visual Basic code. Figure 5.4 Shows the Code Editor window.
Because you want to manipulate the Visual Basic code in the module, open a separate Code Editor window for each module selected from the Project Explorer. In each module, the code in the module is subdivided into separate parts corresponding to the object for each object contained in the module. Use the object list box to toggle between parts. In a form module, the list contains a common segment, a segment that is part of the form itself, and a segment of each control that the form contains. For a class module, the list includes a generic segment and a class segment; For standard modules, only one common segment is displayed.
Each piece of code can contain several different procedures that are accessed using the Procedure list box. For each event procedure for a form or control, the list of procedures for the form module contains a separate segment. For example, the list of procedures for a Label control contains change event segments, Click event segments, and DblClick event segments, and so on. The class module enumerates only the event procedures for the class itself-initialization and termination. Standard modules do not enumerate any event procedures because the standard modules do not support events.

The list of procedures for a generic segment of a module contains only the unique segment-the declaration segment, where module-level variables, constants, and DLL declarations are placed. When you add a child procedure or Function procedure to a module, those procedures are added to the Procedure list box under the Declaration section.
Two different views of the code are available for the Code Editor window. You can view only one procedure at a time, or you can view all the procedures in a module, which are separated by lines (as shown in Figure 5.4). To switch between two views, take advantage of the "View Selection" button in the lower-left corner of the editor window.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.