An application is really nothing more than a set of instructions that directs a computer to complete a task. The structure of an application is the method of organizing instructions, which is where the instructions are stored and the order in which they are executed.
Typical "Hello World" examples and simple applications such as these have simple structures. For only one line of code, the organizational structure is not very important. But the more complex the application, the more obvious is the need for an organization or structure. Imagine how confusing it would be if the application were allowed to execute in random order. Structures also play an important role in how to easily find specific instructions in an application, in addition to controlling the execution of an application.
Because the Visual Basic application is object-based, the code structure of the application is the model that the program physically represents on the screen. By definition, an object contains data and code. The forms that you see on the screen represent properties that define the appearance and intrinsic characteristics of the form. Each form in the application has an associated form module containing its code (the file name extension is. frm).
Each form module contains an event procedure, which is part of the code, with instructions that are executed in response to a particular event. Forms can contain controls. In a form module, you have a corresponding set of event procedures for each control on a form. In addition to event procedures, form modules can contain generic procedures that respond to calls from any event procedure.
You can put code that is unrelated to a particular form or control into another type of module--standard module (file name extension. BAS). A procedure may be used to respond to events in several different objects, and the procedure should be placed in a standard module rather than repeating the same code in the event procedure for each object.
With the class module (file name extension. CLS) To create objects that can be invoked by procedures within the application. A standard module contains only code, and a class module that contains both code and data can be considered as a control without a physical representation.
In the fourth chapter, "Project Management", which describes which parts can be added to an application, this chapter explains how to write code into the various parts that make up your application. By default, the project contains a unique form module. You can add forms, classes, and standard modules as needed. The Nineth chapter "Programming with Objects" will discuss class modules.
How event-driven applications work
An event is an action that is recognized by a form or control. When responding to an event, the event-driven application executes the basic code. Each form and control in Visual Basic has a predefined set of events. If one of the events occurs, and the code exists in the associated event procedure, Visual Basic calls the code.
Although objects in Visual Basic automatically recognize predefined sets of events, it is the responsibility of programming to determine whether they respond to specific events and how to respond to specific events. The Code section (that is, the event procedure) corresponds to each event. When you want the control to respond to an event, write the code into the event procedure for the event.
Objects are recognized for a variety of event types, but most types are common to most controls. For example, most objects recognize the Click event--If you click the form, execute the code in the form's Click event procedure, or if you click a command button, execute the code in the command button's clicking event procedure. The actual code in each case is almost entirely different.
Here is a typical sequence of events in an event-driven application:
1. Start the application, load and display the form.
2. A form (or a control on a form) receives events. Events can be raised by a user (such as a keyboard operation), can be raised by the system (such as a timer event), or indirectly by code (for example, the Load event when the code loads the form).
3. If there is code in the corresponding event procedure, execute the code.
4. The application waits for the next event.
Note that many events occur along with other events. For example, the MouseDown, MouseUp, and Click events also occur when the DblClick event occurs.