In many c/sProgramYou can edit, query, and print data in the database. In the past, different data tables were edited, queried, and printed separately. Each form was brand new. We will sniff"CodeRepetition makes it difficult to maintain and modify code in the future. This may be worse if it is a multi-person project, because each pair of programmers may provide different interface methods for editing, querying, and printing.
In response to the above problems, I have made some adjustments in my previous projects and I will share them with you.
Since the data editing, query, and printing functions appear in each data form, such as "Order Form", "warehouse receiving form", and "Purchase Order Form", the data is uniformly planned. A pop-up dialog box is usually used to edit, query, and print databases. Therefore, a dialog box form class is defined in the project.TdialogformThis class initializes the font, size, and position of the form in a unified manner, and provides an abstract MethodExecuteFor external calls. The Edit, query, and print forms inherit from the form and implement this abstract method.
We addedEdiror,SearcherAndPrinterThree attributes are used to specify the edit, query, and print form instances used.Doedit,DosearchAndDoprintThree methods are used as call interfaces. Place three buttons (toolbar buttons or menus, as needed) in the data browsing form and click the event to call them. Because the code is similar, print is used as an example here:
If Assigned (fprinter) then begin
Printer. Dataset: = Activedataset;
Printer. Execute;
End;
Else
Tdialogbox. Info ( ' Not assigned printer ' );
Now, the basic framework for editing, querying, and printing is ready. The following describes the details.
Different data tables have different fields, so the interface for editing, querying, and printing forms is naturally different. Careful friends may have discovered that there isDatasetAttribute. We use this property to obtain field information and use the dynamic creation control method to create elements on the form that can be input by users. The specific code is not written here.
This method makes full use of the inheritance and polymorphism in the object-oriented programming, minimizes the repeated code in the program, and reduces the maintenance and modification costs in the future.
Let's talk about it.