Create and display order in Delphi to implement custom forms (2)
Although the above method can achieve dynamic selectively creation and display form, there is a fatal weakness, which is the transfer of the program shutdown, and cannot retain the FORM1 program shut down permission. How do you really have a choice creation and display form, and can keep Form1 as the main form? Methods as below:
Place the three forms in Auto-Create Forms, that is, 3 forms are automatically created.
Set a global variable in Form1
VAR
Flag: integer;
Then change the above FORM1 channel as follows:
Procedure TFORM1.FormCreate (Sender: TOBJECT);
VAR
Randomnum: integer;
Begin
Flag: = 0;
Label1.caption: = 'Form1 CREATE COMPLETE!';
Randomize;
Randomnum: = random (10);
IF (randomnum> 0) and (randomnum <= 4) THEN
Flag: = 2
Else IF (Randomnum> 4) and (randomnum <= 8) THEN
Flag: = 3
Else
Flag: = 1;
END;
(Code 6)
Then, add the following code in the oncreate () event of FORM2:
Procedure TFORM2.FORMCREATE (Sender: TOBJECT);
Begin
Label1.caption: = 'FORM2 CREATE COMPLED!';
END;
Add the following code in the oncreate () event:
Procedure TFORM3.FormCreate (Sender: TOBJECT);
Begin
Label1.caption: = 'FORM3 CREATE COMPLETE! (' INTOSTR (FLAG) ')';
Form3.hide;
IF flag = 3 THEN
Begin
Form3.Visible: = true;
End
Else if Flag = 2 THEN
Begin
Form2.visible: = true;
End
Else if Flag = 1 THEN
Begin
Form1.show;
END;
(Code 7)
Also remember to change the Form2 and Form3's FormStyle properties to FSSTayont, set the FormStyle property of the Form1 to FSNORMAL, otherwise the desired form cannot be displayed at the front end of the screen. Set the FORM2 and FORM3's Visible property to false, otherwise the program will not be able to correctly FOCUS (focus) on the desired form. After such modifications, you can select the form that is finally displayed according to the random results.
To analyze the above process, the form 1, 2, 3 is created in order, but set a global variable as a mark of the record in the creation process of Form1, which forms should be displayed, FORM2 creation The process did not do any action, just a simple display information, and the value of the flag flag is determined during the creation process of Form3, which is determined which form should be displayed.
Note that the main form will be displayed, which is also the reason why Form1's Visible is not set to false, because even if it is set to FALSE, it will be displayed.
The above methods, the three forms have been completely created, just one or both of them have not been displayed (set to Visible for false), if now open Spy , you can clearly see 3 The window is there. But our purpose is not only this, but we also have to achieve the dynamics and selectivity of the form. As mentioned earlier, the use of AppLiation.createForm () is used to dynamically create a form, which is the so-called transfer. In fact, we can change this situation, the method is as follows:
Create only Form1 in Project1.dpr
Begin
Application.INITIALIZE;
Application.createform (TFORM1, FORM1);
Application.run;
End.
The code and code 5 in Form1 on onCreate () are. FORM2, FORM3 onCreate () does not require any code, at this time, the offline of the program is still not returned to the Form1's hand, in fact, we only need to add a sentence in Form1's onclose ():
Application.Terminate;
If this sentence is not added, turn off the Form1 does not turn off the entire application, and you must turn off Form2 or Form3. Some people may ask, why not free is Terminate? Check a Delphi's help file About Free's description, with a precautions as described below:
Warning: Never explicitly free a component within one of its own event handlers or the event handler of a component it owns or contains For example, do not free a button, or the form that owns the button, in its OnClick event handler..
It can be seen that this component cannot be explicitly released in a component itself in Delphi. That's why I can't use Application.free. You must use Application.Terminate this sentence to release the resource created by the form, otherwise the resource does not have an error not released. How do I know that the resources are not released? In the Delphi compiler, it is reflected in the Running status and needs to end the program through Run-> Program RESET; Task Manager, the Project is still in the process, occupying the memory and CPU, need to remove it through the end process.
About Application.Terminate, the help file of Delphi has such a description:
.
Here, Delphi tells us that call Terminate instead of Free, allows the application to close in a neat mode.
Here our tasks have not ended yet, we just let Form1 and Form2 or Form3 have the right to turn off the program, we still have to deprive Form2 or Form3 program closing rights, the method is on Form2 and Form3 onclose () events Aplication.run is added to the function, so that there will be no response by the shutdown button of Form2 and FOMR3, and of course, it will not close the program. We add a button to "Close" form, add a FORM2.Visible: = false in its OnClik event;
The so-called closed is actually turned off, not the form of the form, and the resource of the form is not released. Delphi's help files About Application.Run's description does not encourage us to use Run, but from the actual usage situation, there is nothing wrong, just the function of the closure button of the form is blocked, but we have alternatives, Set the form's Border attribute to bsnone to remove the list of Title title columns, with other buttons, or other controls instead of minimization, maximize, and close the button on the title bar.
So far, we can say that it is basically solved the problem starting with this article, but there is an important issue that we ignore, FOM1 will always appear, smart, you may think of some code to process some code in Project.dpr, For the pole, please see the following code:
PROGRAM Project1;
Uses
Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, stdctrls,
Unit1 in 'unit1.pas' {form1},
Unit2 in 'unit2.pas' {form2},
Unit3 in 'unit3.pas' {form3};
{$ R * .res}
VAR
Randomnum: integer;
Begin
Application.INITIALIZE;
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
Application.createform (TFORM2, FORM2);
End
Else IF (randomnum> 4) and (randomnum <= 8) Then // If the random number is between 4 and 8
Begin
Application.createform (TFORM3, FORM3);
End
Else // If the random number is between 8 and 10
Begin
Application.createform (TFORM1, FORM1);
END;
Application.run;
End.
After implementing the above code, Form1, Form2, Form3 do not write any processing code, you can solve our problem without modifying any properties, we may have to sigh, why didn't you think so?
three. to sum up
Using AppLiation.createForm () to dynamically create a form has its limitations, this article describes a simple and practical method to dynamically display the form, the key to this method is to set a flag, depending on the actual situation Assignment, wait until all forms are created, determine the form you want to display according to the set flag. But the disadvantage of this method is that all forms will be created. This paper introduces another method, which can truly realize dynamic selection to create a form, but there is also a shortcoming, that is, the program shutdown permission is average, but we have a corresponding solution. These two methods have their own advantages and disadvantages, for this purpose we use the third approach, write code to implement the function of the form in Project.DPR, and then solve the problem that this article starts. If the reader has better suggestions or solutions, welcome to advise, my email is allencnj@163.com.