Using RAD Studio, the following procedure creates a modal form at design time that is displayed later during program execution.
Building this VCL application consists of the following steps:
- Create the project directory.
- Create two forms for the project.
- Link the forms.
- Create a control on the main form to display the modal form; then write the event handler.
- Build and run the application.
To create the two forms
- Choose FileNewOtherDelphi Projects or C++Builder Projects and double-click the VCL Forms Application icon. The VCL Forms Designer displays Form1.
- Choose FileNewOtherDelphi ProjectsDelphi Files or FileNewOtherC++Builder Files and double-click the Form icon. The VCL Forms Designer displays Form2.
To link Form1 to Form2
- For Delphi, select Form1 and choose FileUse Unit. For C++, select Form1 and choose FileInclude Unit Hdr. The Uses Unit dialog displays.
- Select Form2 (the form that Form1 needs to reference) in the dialog.
- Click OK.
For Delphi, a
uses clause containing the unit name Unit2 is placed in the implementation section of Unit1.
For C++, the #include “Unit2.h” directive is added to Unit1.h.
To display Form2 from Form1
- Select Form1, if necessary; then, from the Standard page of the Tool Palette, place a button on the form.
- In the Object Inspector with Button1 selected, double-click the OnClick event on the Events tab. The Code Editor displays with the cursor in the TForm1.Button1Click (Delphi) or TForm1::Button1Click (C++) event handler block.
- Enter the following event handling code:
To build and run the application
- Save all files in the project; then choose RunRun. The application executes, displaying Form1.
- Click the button. Form2 displays.
- Click the X in the upper right corner of Form2. Form2 closes and Form1 becomes the active form.