Overview of properties, methods, and events
Visual Basic's forms and controls are objects with their own properties, methods, and events. You can think of properties as the properties of an object, methods as the object's actions, and events as the object's response. Objects in daily life, such as a child's balloon, also have properties, methods, and events. The properties of a balloon include visible properties such as its diameter and color. Other properties describe the balloon's state (inflated or uninflated) or invisible properties, such as its lifetime. By definition, all balloons have these properties; these properties also vary from balloon to balloon.
Balloons also have methods and movements inherent to them. For example: the inflation method (the action of filling the balloon with helium), the deflation method (expelling the gas in the balloon) and the ascent method (letting the balloon fly away). All balloons have these abilities.
Balloons also have predefined responses to certain external events. For example, a balloon responds to the event of puncturing it by deflating, and responds to the event of letting go by lifting off.
If the balloon could be programmed, the Visual Basic code would look like this. Its function is to set the properties of the balloon:
Balloon.Color=Red
Balloon.Diameter=10
Balloon.Inflated=True
Pay attention to the syntax of the code, first the object (Balloon), then the attribute (Color), and then the assignment (Red). Repeat this statement, but with a different value, to change the color of the balloon. Properties can also be set during the program design phase in the Properties window.
The way to call the balloon is like this:
Balloon.Inflate
Balloon.Deflate
Balloon.Rise5
Its syntax is similar to that of properties, with the object (a noun) followed by the method (a verb). In the third example there is an additional item, called a parameter, which represents the height to which the balloon will rise. Some methods have one or more parameters that further describe the action to be performed.
The balloon's response to an event looks like this:
SubBalloon_Puncture()
Balloon.Deflate
Balloon.MakeNoiseBang
Balloon.Inflated=False
Balloon.Diameter=1
EndSub
The code in this example describes the behavior of the balloon when a puncture event occurs: calling the Deflate method, and then calling the MakeNoise method with the Bang (the sound it emits) as a parameter. Because the balloon is no longer inflated, set the Inflated property to False and set a new value for the Diameter property.
In fact, balloons cannot be programmed, but Visual Basic forms or controls can be programmed. As the programmer, it's up to you. You decide which properties should be changed, which methods should be called, and which events should be responded to to get the look and behavior you want.
Design form
The form object is the basic building block of a Visual Basic application and is the actual window that interacts with the user when running the application. A form has its own properties, events, and methods for controlling its appearance and behavior.
The first step in designing a form is to set its properties. This can be done in the Properties window at design time, or by code at run time.
Pay attention to design time, that is, any time you create an application in the Visual Basic environment, when you can manipulate forms and controls, set their properties, and program their events. Runtime is the time it takes to actually run and interact with your application.
Set form properties
Many properties of a form affect the appearance of the form. The Caption property determines the text displayed in the form's title bar; the Icon property sets the icon that displays when the form is minimized. The MaxButton and MinButton properties determine whether the form can be maximized or minimized. By changing the BorderStyle property, you can control how the form resizes.
The Height and Width properties determine the initial size of the form; the Left and Top properties determine the position of the form based on the upper left corner of the screen. The WindowState property can set the form to be maximized, minimized, or normal size at startup.
The Name property sets the name of the form and is used to reference the form in code. working for the first time
When a form is added in the process, the name of the form is defaulted to Form1; when a second form is added, its name is defaulted to Form2, and so on. It is best to set a meaningful name for the Name property, such as naming an entry form frmEntry.
The best way to become familiar with these form properties is through practice. Change some properties of the form in the Properties window (Figure 3.3), then run the application and observe the effects of the modifications. If you want to learn more about each property, you can select the property and press F1 to view the online help.
Form events and methods
Forms are objects that can execute methods and respond to events.
Whether due to user interaction or resizing the form through code, a Resize event will be triggered. When the form size changes, you are allowed to move or resize controls on the form.
Whenever a form becomes the active form, an Activate event is generated; when another form or application is activated, a Deactivate event is generated. These events are convenient for initializing or ending form behavior. For example, in the Activate event, you can write code to highlight the text in a specific text box; in the Deactivate event, you can save the changes to a file or database.
To make a form visible, call the Show method:
Form2.Show
Calling the Show method has the same effect as setting the form's Visible property to True.
Many methods of a form call text or graphics. PRint, Line, Circle and Refresh methods can
Used to write and draw directly on the form surface. These methods and more are discussed in detail in Chapter 12, "Working with Text and Graphics."
For more information about forms, see "Revisiting Forms" in Chapter 6, "Creating the User Interface."
->