VFP objects and classes

xiaoxiao2021-03-06  122

Explain the vfp programming of the object, SQL SELECT, C / S development, API program, etc.

First lecture

Objects and classes - This chapter briefly illustrates the basic concepts and characteristics of some objects and classes

I. Basic Concept

What is object? In VFP, all user interface elements (forms, tables, text boxes, labels, etc.) are objects, and the Custom classes for controlling, in runtime are also objects. Objects have their own properties and methods. What is an object of the object and method? The property is a special "variable" describing object features or saves specific information. The method is composed of VFP code, which belongs to a particular "process or function" for a particular action. It can be easily understood as variables, and the method is understood to be a process or function. What is a class? About what is a variety of statements: 1 Class is an object's model 2 is a reusable code 3 is a mold for manufacturing objects. However, it is essentially the same, you just need to remember that "class is reusable code", why do you want to use classes? The purpose of use is mainly to improve programming efficiency. The differences between classes and objects and contacts contain all the properties and methods of the object, which is the "mold" of the object. Objects is the "instance" of the class, and multiple instances can be created by a class. The class itself does not complete any operation, it is only defined the properties and methods of the object, and the actual operation is done by the object it instantiated. Do I have to use classes? You don't have to use classes in your program (such as your program is a program from FoxPro 2x to the VFP, maybe you don't use classes in your program), but if you are using VFP programming You may already use the class. As long as you use the VFP form designer or form Wizard, you consciously unconsciously use the class: If you use the form designer to design your user interface, you are using the base class of the VFP, if you use the form Wizard, Then you use the VFP in advance for your design, these classes are mainly in the wizards directory under the VFP home directory, but you may not know. Features 1 Package 2 Subclass 3 Inherits Some Benefits Some Benefits 1 Hidden Unnecessary Complex 2 Hide Unnecessary Complexity 2 Take advantage of the reuse of the existing function 3 to reduce the difficulty classification of code maintenance 1 Classified by Features Classification Classification Classification Application Class Management Class Packaging Business Class by Use

Second. Basic operation

1 Creating a new class in a new class, following the actual example, how to create a new custom form class To create a new class, follow these steps: a New project named IT, and in the project manager Select "Class" tab (as shown below)

b Click the "New" button to call up the New Class dialog box and then press the necessary information in the dialog box:

This will create a class library called MyFormvcx.vcx in the current directory and create a form class called MyFormModal in this class library.

Then click the "OK" button in the New Class dialog. At this point, the VFP closes the New Class dialog and checks the new class in the Class Designer.

2 Create a new class in an existing class library

The creation step is the same, just when the content stored in the New Class dialog box, click the three button on the right side of the text box and select a class library file that exists on the disk (.vcx )

3 Add custom properties for the class

Enter the "Class" tab of the project manager, select the "MyFormModal" class in the MyFormvcx.vcx class library and click Modify, which will enter the VFP class designer.

After you enter the class designer, the system menu will add a menu bar named "class". Please note that when you close the browser, the menu bar will automatically turn off

From the "Class" menu bar, select the "New Properties" dialog box of the VFP, enter the new property name CPrimaryTable we want to create in the Name dialog box of the New Attribute dialog. , Please note that we have said that the attribute is the variable, so the naming mode of the attribute should follow the variable naming rules, and because it is an attribute of the form, there is no need to use the first character in the variable naming method - scope Identifier.

Also, the attribute name of the new attribute is all lowercase in the property window of the form or control, so the case where the enclosure input here is not affected by the value of the attribute name displayed in the attribute window, which is here with CPrimaryTable. Such formats are to clearly know the role and data type of this property when entering the attribute name. So it is not necessary. In addition, in order to facilitate identification, we change our form class's CAPTION attribute value to MyFormModal by default Form1.

Revete the WindowType property of the form from the default 0 to 1

4 Add a custom method for the class

To create a custom method for a form class, follow these steps:

Enter the "Class" tab of the project manager, select the "MyFormModal" class in the MyFormvcx.vcx class library and click Modify, which will enter the VFP class designer.

Select New Method menu item from the Class menu bar, which will call the VFP's New Method dialog box, enter the new one we want to create in the Name dialog of the New Method dialog box. Attribute name first, previous, next, last (Note: Enter one at once, then click "Add" button), then click the "Close" button to close the New Method dialog.

Then double-click the table tuning the VFP class code editing window, select the following method from the "Procedure" drop-down list of the class code window and paste the related code of each method to the respective method code:

Method First Use Location Recording Pointer to the first record of the table

Local M.lcoriwam.lcoriwa = this.cprimarytablesLect & lcoriwago Topthis.refresh ()

Method PREVIOUS Pipe Location Recording Pointer to the previous record of the current record of the table

Local m.lcoriwam.lcoriwa = this.cprimarytableslect & lcoriwaif! Bof () Skip -1 this.refresh () else Wait Window Nowait "Removed file header" ENDIF

Method NEXT Purpose Location Recording Pointer to the current record of the table

Local m.lcoriwam.lcoriwa = this.cprimarytableselect & lcoriwaif! EOF () Skip if EOF () WAIT WINDOW NOWAIT "Skip -1 Endif this.refresh () Endif

Method LAST use positioning record pointer to the last record of the table

Local m.lcoriwam.lcoriwa = this.cprimarytablesLect & lcoriwago bottomthis.refresh ()

Also paste the following code in the INIT of the form class:

thisform.cprimarytable = alias ()

5 use an existing form class to create a new form

Three ways

method one

In order to use the form class created in the previous step, follow these steps:

1 From the "Tool" -> Options "tab, click the Add button and select the" MyFormvcx.vcx "library created earlier so that the" MyFormvcx.vcx "class library is registered.

2 Enter the project manager to select a new form, and the VFP will create a new form for you. From the Form Control Toolbar, "View Class" and select MyFormVCX, at this time, the Form Control Toolbar becomes as shown below Down:

3 Click the MyFormModal form class on the toolbar, then click Form1 in the form designer, and the VFP will ask you "To add a form, you need a form set object, create one?", Select "Yes", At this point, the VFP will place one on the original form, derived on the MyFormModal form class (it will cover on the original form, you can move the newly added form, so you can see that there are two forms in the form. A form of forms) 4 Select the original form (generally form1), and select the removal form from the "Form" menu, the VFP will ask "Is the form FORM1?", Select "Yes". And then " The Form "menu Selects" Remove Form Set ", when the VFP is asked if you remove the form set, this is selected from the MyFormModal form class in the" MyFormvcx.vcx "class library, you created a new form. .

Method Two

1 From the Template class area in the form tab in Tools, "Options, check the" Form "check box, the VFP prompts you to select a VCX file and the form class.

2 Select the "MyFormvcx.vcx" class library and select the MyFormModal form class in the "Class Name" list box. Click OK twice to close the Form Template dialog and "Option" dialog.

3 Enter the "Document" tab of the project manager and click the "New" button to create a form, select "New Form" in the New Form dialog box of the VFP, so that the VFP will be from the "MyFormvcx.vcx" library MyFormModal form class creates a new form for you

Method three

Create a new form from the MyFormModal form class in the MyFormvcx.vcx class library with the following command:

Create form mynewform as myformmodal from MyFormvcx

6 subclass an existing class

Subclass means that it is born from a class, which has all methods and properties of their parent classes. In general, only when the function of the father cannot meet the needs. Subclasses for existing classes, and further modifications to subclasses.

1 Select "Class Browser" from the Tools menu

2 Click the "Open" from the toolbar of the Class Browser and select the "MyFormvcx.vcx" class library.

3 Select the MyFormModal form class and click the "New Category" button, and the VFP will ask you, derived from that class, stored in a VCX file, here, you can fill the MyFormModalNotitle at the "class name", derived The MyFormModal class is stored in the MyFormvcx.vcx class library, and finally click OK, which is a new class class named MyFormModalNotitle from the MyFormModal form class class.

7 Add existing classes to your form and use your own class to process data

I. In order to add an existing class to the form, create some command buttons in the "MyFormvcx.vcx" class library as follows:

1 Click the "Open" button in the File menu and select the project file "IT.PJX" we previously created to enter the project manager.

2 Click the Class tab to enter the class tab of the project manager, if there is no content in the class tag, click the Add button and select the MyFormvcx.vcx class library to add it to the project.

3 Select MyFormVCX and click the "New" button, which will display the New Class dialog.

4 Enter the name cmdfirst of the new command button class we want to create, select CommandButton from derived in the drop-down list box, then click OK, this will enter the class designer

5 Modify the following properties and methods of our new command button class CAPTION: 1

Enter the following code in the Click:

thisform.first

Create the following command button in a similar method:

cmdlast

CAPTION: The last one

Enter the following code in the Click:

ThisForm.last

CMDPREVIOUS

CAPTION: Previous

Enter the following code in the Click:

thisform.previous

CmdNext

CAPTION: Next

Enter the following code in the Click:

thisForm.next

The class library after the creation of the command button is shown below:

Second, the above steps, keep the project manager still open

1 Enter the following command in the Command window to create a form that is derived from MyFormModal form:

Create form mynewform as myformmodal from MyFormvcx

This will display a newly created form in the form designer, narrow some of the form designer and drag it with the project manager to open some locations, as shown below:

2 Drag four different command buttons directly from the class tags in the project manager to the appropriate location on the form:

The form after completion is shown below:

3 Add a table to the form of the data environment: Right click on the form and select the "Data Environment" from the pop-up menu, the VFP will prompt you to open a table, you just select a table and click OK to close the "Open" dialog, then Click the Close button in the Add Table or View dialog to close the Add Table or View dialog.

4 Drag some fields from the data environment to the form, as shown below:

4 Save the form file and switch to the Document tab of the Project Manager, click the Add button to add the form MyNewForm.scx we just created to the project.

5 Select the form mynewform and click the "Run" button to run the form we just created, you can click the different command buttons to view the running effect. Although there is no code in this form, each command button can work This is one of the benefits of working with classes.

8 Save an existing form into an existing class library

Just open an existing form and select Save from the File menu as a class to save it as a form class.

9 Save one or more controls on the form into a class library as class

Just open an existing form, select one or more controls and select Save from the File menu to save the selected object as a container class.

转载请注明原文地址:https://www.9cbs.com/read-126426.html

New Post(0)