### ☆Working Mechanism of VB Programs
Visual Basic is an object-based programming language. A Visual Basic program consists of numerous objects and code that responds to various events. When a user performs certain operations on an object, a specific event is generated. Objects can also automatically generate events under certain specific conditions. When an event occurs, the Visual Basic program automatically calls the corresponding code to handle these events. It can be said that during the running process of the application, it is always waiting for the occurrence of events and responding to them until the user ends the entire program.
●
Many people feel particularly difficult to understand as soon as they come into contact with the term "object-oriented". The word "thing" is commonly used in daily spoken language, and it can be simply considered that the "thing" mentioned here has the same meaning as the "object" here. An object is a very specific existing thing, such as a pencil, a generator, a tiger (if expressed in spoken language, these things are all "things"). It is not difficult to find that whether it is a pencil, a generator, or a tiger, they are all a thing with complete meaning: if a pencil is missing the lead core, it is no longer a pencil; if a generator is missing the magnetic core, it cannot generate electricity and thus cannot be called a generator.
In daily life, how to distinguish "things"? First, we classify things. Obviously, we will not confuse a home computer with a lathe because they belong to two different types of things. Then, for objects of the same type, different individuals can be distinguished by their different characteristics. For example, two pencils, they are all objects of the same type, but they may be distinguished by color: one is yellow and the other is red.
Thus, in the object-oriented theory, the term "class" is used to represent different types of objects, and attributes and methods are used to represent different characteristics of the same type of objects. Attributes are the static characteristics of an object. For example, color (this is static) is an attribute of a pencil. The color of a pencil can be black, red, or blue. Methods are the dynamic characteristics of an object. For example, generating electricity (this is an action, which is dynamic) is a method of a generator. The method of an object is triggered by an external event. For example, the power generation method of a generator will only be triggered and enter the power generation state after the operator allows the generator to operate.
In Visual Basic, forms, controls, menus, etc. are all objects. The objects used in the example program "Welcome to the VB World" are: one form, two command button controls, and one label control. (You can imagine it, here it follows the text in the book)
Take the command button control as an example. In the example program, two button controls are used. Although these two objects are both command buttons (that is, belong to the same class), they have different Caption attributes: one is "Start" and the other is "Exit". They also have different Name attributes: one is "Command1" and the other is "Command2". The command button control has many methods, such as the "move" method. When the position of the command button control on the form needs to be changed, we call the "move" method of the command button to achieve it.
Obviously, in Visual Basic programming, the user interface of the application is composed of many objects related to the user interface. Therefore, the first thing to do when writing a Visual Basic program is to prepare the objects to be used in the program. (Prepare "rice"--|thing|to go with rice)
●
The main body of a Visual Basic program is numerous objects, that is, forms, controls, menus, etc. When a user performs an operation on an object, such as a mouse click on a command button, a mouse click event will be triggered on this command button object. Generally speaking, the user's operations are purposeful, and the application should respond to the user's operations, that is, the programmer should write code to handle these events.
In fact, the running process of a Visual Basic program is the process of handling events. When the program runs, various different objects will generate many different events. Some of these events are caused by user operations, and others are generated by the object itself under certain conditions (such as the "timer time is up" event). Programmers write processing code for various events respectively. When no event occurs, the program does nothing. This is the so-called event-driven mechanism.
This event-driven mechanism based on objects in Visual Basic is a magic weapon for simplifying Windows program design. Under this mechanism, to design a Visual Basic program, you only need to follow the following steps:
1) Design the user interface of the program, that is, place various objects.
2) Define the attributes of each object in the user interface, such as color, title, etc.
3) Define each possible event to be used.
4) Write corresponding code for the events.
Among them, the first 3 steps are steps related to interface design. Programmers can complete these designs only by using VB's visual tools, and no code needs to be written at all. (Of course, it can also be written manually, and VBS has to be written manually) The 4th step is the coding work to realize the program function. In this way, programmers can focus most of their energy on the realization of functions. As for how the interface captures user operations, how to issue events, and how to call the event handling code written by the programmer, there is no need to care. VB has already designed these things for the user.
[ Last edited by Billunique on 2007-5-16 at 06:08 PM ]
★①②③④⑤⑥⑦⑧⑨⑩㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ【●】→←↑↓▲