The use of VB Objects (i)

Source: Internet
Author: User
Tags manual

Working with objects

When you create an application in Visual Basic, you are dealing with objects. You can use objects provided by Visual Basic, such as controls, forms, and data access objects. You can also control another application object within a Visual Basic application. You can even create your own objects, and define their properties and methods.
What is the object
An object is a combination of code and data that can be handled as a unit. objects can be part of an application, such as a control or a form. The entire application is also an object. The following table lists several types of objects that you might use in Visual Basic:

Where the object comes from. Each object in visual Basic is defined with a class. The relationship between the object and its class is understood by analogy between the cookie mold and the cookie. Cookie mold is a class. It determines the characteristics of each cookie, such as size and shape. Creates an object with a class. The object is the cookie.
Here are two examples to further illustrate the relationship between classes and objects in Visual Basic.
1. On the Toolbox of Visual Basic, a control represents a class. They do not actually exist until these objects, called controls, are drawn on the form. When you create a control, you copy the control class or create an instance of the control class. This class instance is the object referenced in the application.
2. A form that operates at design time is a class. At run time, Visual Basic establishes a class instance of the form. The Properties window displays the class and name properties of objects in the Visual Basic application, as shown in Figure 5.8.

The object is regarded as an exact replica of the class, thus creating all objects. Once they are in the form of a single object, the properties can be changed. For example, if you draw three command buttons on a form, each command button object is an instance of a command button class. Each object has a set of common features and functions (properties, methods, and events) defined by the class. However, each object has its own name, can be set to be valid or invalid, can be placed in different positions of the form, and so on.
For simplicity, most of the content outside this chapter will not refer to the object's classes too much. For example, just remember that the term "ListBox control" means "one instance of a list box class."

What can I do with the object?
Object can provide ready-made code, save writing trouble. For example, you can create a dialog box for opening and saving files yourself, but you don't have to. Instead, use the CommonDialog control (an object) that is provided by Visual Basic. Although users can also write programs for scheduling and resource management, they don't have to. You can also use the calendar, resources, and Task objects provided by Microsoft Project here.

Visual Basic can combine objects from other sources
Visual Basic provides a tool that combines objects from different resources. You can now combine the various powerful features of visual Basic with applications that support automation, formerly known as OLE Automation, to create customized solutions. Automation is an attribute of part object mode (COM), an industry standard used by applications to display objects to develop tools and other applications.
You can combine controls within Visual Basic, or you can use objects provided by other applications. Consider putting the following objects into the Visual Basic form: 1.Microsoft Excel Chart objects
2.Microsoft Excel Worksheet Object
3.Microsoft Word Document Object
These objects can be used to create a checkbook application as shown in Figure 5.9. Because you don't have to write code to rebuild features that have been provided by objects such as Microsoft Excel and Word, you save a lot of time.

Initial use of objects
Visual Basic objects support properties, methods, and events. In Visual Basic, the data (settings and properties) of an object are attributes, and a variety of procedures that can be manipulated on an object are called methods. An event is an action that can be recognized by an object, such as clicking a mouse and pressing a keyboard key, and writing code to respond to an event.
Changing the properties of an object can change the attributes of the object. One of the properties of a radio, for example, is the volume. In the jargon of Visual Basic, the radio has a "Volume" attribute, changing its value to adjust the volume size. It is assumed that the volume value of the radio can be set between 0 and 10. If you can control the radio through Visual Basic, you can write code in one procedure, raising the "Volume" property value from 3 to 5 to make the sound louder:
Radio.volume = 5
In addition to attributes, objects have methods. Both methods and properties are part of the object. Generally, the method is the action to be performed, and the attribute is the attribute to set or retrieve. Take a dial-up call for example. You can say that the telephone has a "dial" method, the syntax for dialing a 7-digit phone number is:
Phone.dial 5551111
Object and an event. An event is triggered when there is a change in some aspect of the object. For example, the radio may have a "volumechange" event. The phone may have "ring" events, and so on.

Controlling Objects with attributes
Individual properties vary depending on the time that you can set and get their values. Some properties can be set at design time. You can set the values of these properties in the Properties window without writing any code. Some properties are not available at design time, so these properties are set at run time only through code.
Properties that can be set and get values at run time are called Read and write properties. Properties that can only be read at run time are called read-only properties.

Setting property values
Set the value of a property when you want to change the appearance or attributes of an object. For example, by changing the Text property of a TextBox control, you can change the contents of the text box.
Set property values with the following syntax:
Object.property = Expression
The following is the statement that sets the property:
Text1.top = 200 ' Sets the top property to 200 twips (twips).
text1.visible = True ' Displays the text box.
Text1.Text = "Hello" shows "Hello" in the text box.

Reading property values
To read the state of an object before the code performs an additional action, such as assigning a value to another object, reads the property value. For example, you can return the Text property value of a TextBox control before you run the code to determine the contents of the box, where the code may change the value.
In most cases, you can obtain property values in the following syntax:
variable = object.property
Property values can be part of a more complex expression without having to assign a property to a variable. The following code calculates the top property of a new member in the control array, which equals the first property of the previous member plus 400.
Private Sub Cmdadd_click ()
' [statement]
Optbutton (n). top = Optbutton (n-1). Top + 400
' [statement]
End Sub
Tip If you use a property value more than once and store that value in a variable, the code executes faster.

Use methods to perform actions
Method can affect property values. For example, in the example of a radio analogy, the SetVolume method changes the Volume attribute. Similarly, in Visual Basic the list box has the List property, and the clear and AddItem methods can change this property.

Using methods in your code
How to write a statement when you use a method in your code depends on how many parameters the method requires and whether it returns a value. If the method does not require parameters, write the code with the following syntax:
Object.Method
In the following example, use the Refresh method to repaint the picture frame:
Picture1.refresh ' forces repaint the control.
Some methods, such as the Refresh above, have no parameters and no return value.
If the method uses more than one argument, separate them with commas. For example, the Circle method uses parameters that represent the position, radius, and color of the circle on the form:
' Draw a blue circle with a radius of 1200 twips.
Form1.circle (1600, 1800), 1200, Vbblue
If you want to save the return value of the method, you must enclose the arguments in parentheses. For example, the GetData method returns a picture from the Clipboard:
Picture = Clipboard.getdata (Vbcfbitmap)
If there is no return value, the argument does not appear in parentheses. For example, the AddItem method does not return a value.
List1.AddItem "Yourname" adds "yourname" to the list box.
For more information about the syntax and parameters that Visual Basic provides for all methods, see the VisualBasic 6.0 Language Reference manual.

The relationship between objects
When you place two command buttons on the same form, they are two separate objects with different Name property set values (Command1 and Command2), but share the same class--the command button.
They also share the feature that they are on the same form. As you've seen earlier in this chapter, controls on a form are also included in the form. This puts the control on one level. To reference a control, you must first refer to the form, just as you would dial a telephone call by dialing the country or region number, and then dialing the specific phone number.
These two command buttons also share the feature that they are all controls. All controls have a common attribute that makes them different from the form and other objects in the Visual Basic environment. The following sections explain how Visual Basic uses collections to group related objects.

Hierarchy of objects
The object hierarchy provides an organizational structure that determines the interrelationships between objects and the ways in which they are accessed. In most cases, you do not have to consider the level of the Visual Basic object. But:
1. When manipulating objects in other applications, familiarize yourself with the object hierarchy of that application. For information about locating object hierarchies, see Chapter Tenth, "Programming with parts."
2. When using data Access objects, you should be familiar with the hierarchy of data access objects. In Visual Basic, a general situation in which an object contains other objects often occurs. We'll discuss it in detail below.

Using Object collections
The collection object has its own properties and methods. Objects in the collection of objects are referenced as members of the collection. Each member in the collection is numbered sequentially from 0, which is the index number of the member. For example, the control collection contains all the controls on a given form, as shown in Figure 5.10. If you must perform the same operation on all objects in the collection, you can simplify the code with the collection.

For example, the following code iterates through the control collection and lists the names of each member in the list box.
Dim MyControl as Control
For each mycontrol in Form1.controls
' Send the name of each control to the list box.
List1.AddItem MyControl.Name
Next mycontrol

Apply the properties and methods of a collection member
There are two common ways to address collection Object Members:
1. Specify the name of the member. The following two expressions are equivalent:
Controls ("List1")
controls! List1
2. Use the index number of the member:
Controls (3)
Once you are able to address all members as a whole, and you can address individual members individually, you can apply properties and methods in any of the following ways:
' Sets the top property of the ListBox control to 200. controls! List1.top = 200
Or
Dim MyControl as Control
For each mycontrol in Form1.controls ()
' Sets the top property of each member to 200.
Mycontrol.top = 200
Next mycontrol

Objects that contain other objects
In Visual Basic, some objects contain other objects. For example, a form usually contains one or several controls. The advantage of using an object as a container for other objects is that referencing the container in your code makes it clear which object to use. For example, figure 5.11 illustrates that there can be two different forms in one application, one for entering accounts payable transactions and the other for entering accounts receivable transactions.

Two windows can be named a istacctno list box. You can specify exactly which list box to use by referencing a form that contains a list box: FrmReceivable.lstAcctNo.AddItem 1201
Or
FrmPayable.lstAcctNo.AddItem 1201

Public collections in Visual Basic
In Visual Basic, a general situation in which an object contains other objects often occurs. The following table briefly describes the most commonly used collections in Visual Basic.

Object control can also be implemented in Visual Basic.
For more information about object containers, see "Using collections instead of arrays" in chapter eighth "re-programming". For information about the printer collection, see Chapter 12th, "Working with text and graphics." For more information about form collections and control collections, see the Visual Basic 6.0 Language Reference Guide.

Container properties in the window, you can change the object's container with the Container property. The following controls can contain other controls:
1.Frame Control
2.PictureBox Control
3.ToolBar controls (for Professional and Enterprise editions only)
This example demonstrates how to move a command button from one container to another on a form. Open a new project and draw a Frame control, PictureBox control, and CommandButton control on the form.
The following code, in the form-click event, increases the counter variable and moves the command button between the containers through a selectcase loop.
Private Sub Form_Click ()
Static IntX as Integer
Select Case IntX
Case 0
Set Command1.container = Picture1
command1.top= 0
command1.left= 0

Case 1
Set Command1.container = Frame1
command1.top= 0
command1.left= 0

Case 2
Set Command1.container = Form1
command1.top= 0
command1.left= 0

End Selectint
X = IntX + 1
End Sub
For more information, see "Container Properties" in the Visual Basic 6.0 Language Reference manual.

Communication between objects
In addition to using and creating objects in Visual Basic, you can communicate with other applications and manipulate objects of other applications in your own applications. The ability to share data between applications is one of the key capabilities of the Microsoft Windows operating system. With visual Basic, communication with other applications becomes extremely flexible.
For more information about using objects from other applications and their communications, see chapter Tenth, "Programming with parts."

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.