Using programs in Delphi to implement custom form creation and display order (1)
Abstract: This article describes a simple and practical method to dynamically create and display the form, write code to implement the function of the form in Project.dpr, and create, create, run, run In-depth discussion on the mechanism of the end.
Keywords: Delphi Form Creation Display Dynamic Selection
One. statement of problem
In Delphi, we can create multiple forms through two ways and set their creation order. The first method is to appear after the newly added multiple forms, pressing Project-> Options on the Delphi toolbar ... Forms One page Sets the creation order of the form. As shown below:
The second method is to modify in the engineering file of Delphi, where it is assumed to be Project1.dpr, which will be displayed as follows:
PROGRAM Project1;
Uses
Forms,
Unit1 in 'unit1.pas' {form1},
Unit2 in 'unit2.pas' {form2},
Unit3 in 'unit3.pas' {form3};
{$ R * .res}
Begin
Application.INITIALIZE;
Application.createform (TFORM1, FORM1);
Application.createform (TFORM2, FORM2);
Application.createform (TFORM3, FORM3);
Application.run;
End.
(Code 1)
The order of the creation of 3 forms can be adjusted by adjusting the order of the statements in the above code, the effect is the same as the first method.
However, there is a defect in these two methods, that is, the Delphi compiler defaults to the first created form as a main form, each program starts, first display the main form, and the form of the form is Changeless.
How to make a dynamic change program loading order as needed?
two. Delphi form creation is unveiled
1. Form permission transfer experiment
New 3 FORM, forms Form2 and Form3 in the Form2 and Form3 in Project-> Options ... Prevent Form1 in Auto-Create Forms, let Form1 are created in the program.
Add the following code in the oncreate () event function in the Form1 (main form):
Procedure TFORM1.FormCreate (Sender: TOBJECT);
Begin
Label1.caption: = 'FORM1 CREATE COMPLED!';
Form1.show;
Application.createform (TFORM2, FORM2);
END;
(Code 2)
Add the following code to the oncreate () event function:
Procedure TFORM2.FORMCREATE (Sender: TOBJECT);
Begin
Label1.caption: = 'FORM2 CREATE COMPLED!';
Form2.show;
Application.createform (TFORM3, FORM3);
END;
(Code 3)
Add the following code in the Form3 oncreate () event function:
Procedure TFORM2.FORMCREATE (Sender: TOBJECT);
Begin
Label1.caption: = 'FORM3 CREATE COMPLED!'; FORM3.SHOW;
END;
(Code 4)
This program will display three forms after running, respectively, Form1, Form2, and Form3. You may think, if you want to close the program, you can close the Form1 this main form. However, you are wrong, it should be turned off to close the entire program. why? The key is created in the creteform () form, check the Delphi's random help file clear. The help file regarding the creteform () function is as follows:
Call CreateForm to dynamically create a form at runtime. Developers do not need to add code for creating most forms, because typically one or more calls to CreateForm are added automatically to the project's main source when using the form designer.
Createform Creates A New form of the typeter and assigns it to the variable given by the review object.
Note: by Default, The Form Created by The First Call To Createform in a project Becomes the Application's Main Form.
Special note, the last sentence, by default, the first form created by the createform call in a project is the main form of the application. This is not difficult to explain why the FORM3 needs to be turned off to close the application. Because the first use of CreateForm in the program is a code in Project1.dpr:
Begin
Application.INITIALIZE;
Application.createform (TFORM1, FORM1);
Application.run;
End.
This code is automatically generated in Project.dpr, and then we use CreateForm in Form1 () event function, then the creation of Form1 has not ended yet (because the oncreate event is not completed), the master of the program The form has changed, turned form2, and then we used CreateForm on the oncreate () event function in Form2, and the main form of the program turned to form3, which means the promotion of the program. It has been mastered in Form3 hands. Never add Application.createform () on the oncreate () event function in Form3, otherwise, the program will enter a resource creation dead loop, a little bit out of your machine's memory.
It can also be seen from here that Delphi is not a complete object-oriented, which can create a form anytime, anywhere, anywhere.
2. Form permission reservation
If we want to reserve the programs permission in Form1, you need to create a new button, then put the code of the oncreate () event function in Form1 in the Button's onclick () event. At this time, only the FORM1 is turned off to turn off the entire program.
3. Dynamic selection creation form
Go back to our question, how do you dynamically create a form? We use a random number to simulate the dynamic situation, the actual situation may be a value that the program is dynamically determined when initialization, and this value is determined to display FORM2 or display FORM3, or only Form1. The code is as follows: Procedure TFORM1.FormCreate (Sender: TOBJECT);
VAR
Randomnum: integer;
Begin
Label1.caption: = 'Form1 CREATE COMPLETE!';
Randomize; // Set the machine
Randomnum: = Random (10); // Take the random value between 0 and 10
IF (randomnum> 0) and (randomnum <= 4) Then // If the random value is between 0 and 4
Begin
Form1.show;
Application.createform (TFORM2, FORM2);
End
Else IF (randomnum> 4) and (randomnum <= 8) Then // If the random number is between 4 and 8
Begin
Form1.show;
Application.createform (TFORM3, FORM3);
End
Else // If the random number is between 8 and 10
Form1.show;
END;
(Code)
5
)