Delphi development memoir-object-oriented foundation, inheritance (continued)

Source: Internet
Author: User

declare
var
testform: tfrmbasic In the event;
Create a form object and assign values to it, from the previous Code , we can easily see that writing Programs has many drawbacks, for example, the Code is repetitive and rigid.
in fact, we can use the tag or caption of the trzgroupitem object of the trzgroupbar control to create the corresponding form object, for example:
procedure tfrmmain. rgbasedatumitems0click (Sender: tobject);
begin
createformselect (sender);
end;
the createformselect function is as follows:
procedure tfrmmain. createformselect (Sender: tobject);
begin
msgbox (sender as trzgroupitem ). caption, 0);
end;
Based on the above, we create a function to create a form object in the global unit file: Procedure createform (tbtag: integer; acaption: string);
then call createform in createformselect, as follows:
procedure tfrmmain. createformselect (Sender: tobject);
begin
createform (trzgroupitem (sender ). tag, (sender as trzgroupitem ). caption);
end;
During the createform process, we can assign corresponding objects to the created forms, such as setting the display of controls, assigned to different dataset objects

Next, we put two groups of adoquery and datasource in the data form and name them respectively: dsemployee, qryemployee, dsdepartment, and qrydepartment. Then we set the dataset attribute of dsemployee to qryemployee, the dataset attribute of dsdepartment is qrydepartment, and the SQL attributes of qryemployee and qrydepartment are set:
Select * from employee and select * from department. Of course, you need to create the tables "employee" and "department" respectively in the database.
The createform function creates three types of forms based on different input parameters: basic data forms, document forms, and report forms. The Code is as follows:
Procedure createform (tbtag: integer; acaption: string );
Begin
Case tbtag
100 .. 199:
Begin
Createbasicform (tbtag, acaption );
End;
200 .. 299:
Begin
Createbillform (tbtag, acaption );
End;
300 .. 399:
Begin
Createreportform (tbtag, acaption );
End;
End;
End;
In this Code, we found that createbasicform is called, and this method is the function that we actually create the basic data form. Now let's unveil her secret and see what magic she has.
Procedure createbasicform (tbtag: integer; acaption: string );
VaR
Lablelist, labledetail: string;
Tempstr: string;
Begin
Case tbtag
100:
Begin
Lablelist: = 'employee list ';
Labledetail: = 'employee profile ';
Frmdm. dsmaster: = frmdm. dsemployee;
End;
101:
Begin
Lablelist: = 'department list ';
Labledetail: = 'department information ';
Frmdm. dsmaster: = frmdm. dsdepartment;
End;
End;
Lockwindowupdate (frmmain. Handle );
If assigned (vbasicform) then
Begin
Msgbox ('form creation error! ', Mb_iconinformation );
Exit;
End;
If not assigned (vbasicform) then
Begin
Vbasicform: = tfrmbasic. Create (application );
Vbasicform. Caption: = acaption;
With vbasicform do
Begin
Dbgrdhlist. datasource: = frmdm. dsmaster;
Dxdbinsedit. datasource: = frmdm. dsmaster;
End;
Frmdm. dsmaster. dataset. Active: = true;
With vbasicform. dbgrdhlist do
Begin
Columns. Clear;
Columns. Add;
Columns [0]. Title. Caption: = copy (lablelist,) + 'code ';
Columns [0]. Width: = 100;
Columns [0]. fieldname: = frmdm. dsmaster. dataset. Fields [1]. fieldname;
Columns. Add;
Columns [1]. Title. Caption: = copy (lablelist, 1, 4) + 'name ';
Columns [1]. Width: = 160;
Columns [1]. fieldname: = frmdm. dsmaster. dataset. Fields [2]. fieldname;
End;
Initdatasetinspectorl (frmdm. dsmaster. dataset. Tag );
Vbasicform. show;
End
Else
Vbasicform. show;
Lockwindowupdate (0 );
End;
It can be seen from the code that some variables are assigned values based on input parameters. Pay special attention to the line of code: frmdm. dsmaster: = frmdm. dsemployee;
This line of code is used to assign values to components in the data component form to ensure that data operations in the class tbaseclass referenced in the base form are performed on the same dataset.
Review the operations on data in the tbaseclass class are performed on the dsmaster data source in the data form. Because we need to assign the corresponding data source code to the dsmaster data source in the data form. Continue to read the code. you can know that the dsmaster data source in the data form has been assigned to the dbgrdhlist and dxdbinsedit controls in the creation form. The Code is as follows: dbgrdhlist. datasource: = frmdm. dsmaster;
Dxdbinsedit. datasource: = frmdm. dsmaster;
The following code initializes two data display controls, while dxdbinsedit initializes the initdatasetinspectorl. The source code of initdatasetinspectorl is as follows:
Procedure initdatasetinspectorl (tabletag: integer );
VaR
I, j, tempwidth, maxwidth: integer;
Strtableid, strlist: string;
Tempquery: tadoquery;
Tablevalue: Boolean;
Varrow: integer;
VFL: integer;
Vfieldlist: array [0 .. 19] of string;
Begin
Strtableid: = getfieldname (tabletag, 2 );
Strtableid: = '01 ';
Tempquery: = tadoquery. Create (application );
Try
Tempquery. Connection: = frmdm. condb;
Tempquery. SQL. Clear;
Tempquery. SQL. Text: = format ('select * From datadictcontent where tableid = ''% s' 'order by tableid, fieldid', [strtableid]);
Tempquery. open;
I: = 0;
Maxwidth: = 0;
If vbasicform. dxdbinsedit. totalrowcount> 0 then
Vbasicform. dxdbinsedit. clearrows;
If not tempquery. isempty then
While not tempquery. EOF do
Begin
If tempquery. fieldbyname ('displayflag'). asboolean then
With vbasicform do
With dxdbinsedit do
Begin
Beginupdate;
Defaultfields: = false;
Case tempquery. fieldbyname ('spectorrowtype'). asinteger
0:
Createinspectrow (dxdbinsedit, tdxinspectordbrow, tempquery, 0); // dxdbinsp
1:
Createinspectrow (dxdbinsedit, tdxinspectordbrow, tempquery, 1 );
2:
Createinspectrow (dxdbinsedit, tdxinspectorcomplexrow, tempquery, 2); // dxinspectorcomplexrow reference unit file dxinspct
3:
Createinspectrow (dxdbinsedit, tdxinspectordbmaskrow, tempquery, 3 );
4:
Createinspectrow (dxdbinsedit, tdxinspectordbdaterow, tempquery, 4 );
5:
Createinspectrow (dxdbinsedit, tdxinspectorlookuprow, tempquery, 5 );
6:
Createinspectrow (dxdbinsedit, tdxinspectordbcheckrow, tempquery, 6 );
7:
Createinspectrow (dxdbinsedit, tdxinspectordbcalcrow, tempquery, 7 );
8:
Createinspectrow (dxdbinsedit, tdxinspectordbbuttonrow, tempquery, 8 );
9:
Createinspectrow (dxdbinsedit, tdxinspectordbspinrow, tempquery, 9 );
10:
Createinspectrow (dxdbinsedit, tdxinspectordbpickrow, tempquery, 10); // dxdbinspectordbpickrow reference unit file dxdbinrw
11:
Createinspectrow (dxdbinsedit, tdxinspectorblobrow, tempquery, 11 );
12:
Createinspectrow (dxdbinsedit, tdxinspectordbimagerow, tempquery, 12 );
13:
Createinspectrow (dxdbinsedit, tdxinspectordbtimerow, tempquery, 13 );
14:
Createinspectrow (dxdbinsedit, tdxinspectordbcurrencyrow, tempquery, 14 );
15:
Createinspectrow (dxdbinsedit, tdxinspectordbhyperlinkrow, tempquery, 15 );
16:
Createinspectrow (dxdbinsedit, tdxinspectordbpopuprow, tempquery, 16 );
17:
Createinspectrow (dxdbinsedit, tdxinspectordbmrurow, tempquery, 17 );
18:
Createinspectrow (dxdbinsedit, tdxinspectordbmemorow, tempquery, 18 );
19:
Createinspectrow (dxdbinsedit, tdxinspectordbgraphicrow, tempquery, 19 );
Else
Createinspectrow (dxdbinsedit, tdxinspectordbrow, tempquery, 1 );
End;
Endupdate;
End;
Tempquery. Next;
End;
Finally
Tempquery. Free;
End;
End;
The above method calls the createinspectrow method again. The Code is as follows:
Procedure createinspectrow (vardbinspector: tdxdbinspector; IR: tdxinspectorrowclass;
Varadoquery: tadoquery; rowflag: integer );
VaR
Rowbutton: tdxeditbuttonclass; // dxexedtr, the unit file referenced by the tdxeditbuttonclass type
Varint: integer;
Strlist: string;
J: integer;
Begin
With vardbinspector do
With tdxinspectordbrow (createrow (IR) do
Begin
Caption: = varadoquery. fieldbyname ('displaycaption '). asstring;
Fieldname: = varadoquery. fieldbyname ('fieldname'). asstring;
If varadoquery. fieldbyname ('readonlyflag'). asboolean then
Readonly: = true;
Name: = 'ir' + fieldname;
If rowflag = 8 then
Begin
Varint: = vardbinspector. indexofrow (vardbinspector. rowbyname (name ));
(Vardbinspector. Rows [varint] As tdxinspectordbbuttonrow). onbuttonclick: = frmbasic. dbinspectorrowonbuttonclick;
End;
If fieldname = 'Password' then
Passwordchar: = '*';
If rowflag = 10 then
If varadoquery. fieldbyname ('dictionaryflag'). asboolean then
Begin
If varadoquery. fieldbyname ('dictionarytype'). asboolean then
Begin
// Items [I]. buttonstyle: = ibsauto;
// Items [I]. picklist. Clear;
Varint: = vardbinspector. indexofrow (vardbinspector. rowbyname (name ));
Strlist: = varadoquery. fieldbyname ('dictionarytypeid'). asstring;
J: = pos (';', strlist );
While j <> 0 do
Begin
(Vardbinspector. Rows [varint] As tdxinspectordbpickrow). Items. Add (copy (strlist, 1, J-1 ));
// Items [I]. picklist. Add (copy (strlist, 1, J-1 ));
Strlist: = copy (strlist, J + 1, length (strlist)-j );
J: = pos (';', strlist );
End;
End
// Else
// Items [I]. buttonstyle: = ibsellipsis;
End;

end;
the above two processes have a lot of code, but they are also very simple. You only need to know more about the control tdxdbinspector. It is mainly used to dynamically create data fields. I will not do more here.
the above Code contains the following two lines of code:
strtableid: = getfieldname (tabletag, 2);
strtableid: = '01 ';
obviously, there are some problems. Since the function is assigned a value, why is the strtableid assigned a value of '01?
there are also datadictcontent tables used to initialize and display the data display control tdxdbinspector. For more information, see

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.