Dynamically Creating a VCL Modeless Form

From RAD Studio
Jump to: navigation, search

Go Up to How To Build Windows VCL Applications

A modeless form is a window that is displayed until it is either obscured by another window or until it is closed or minimized by the user. Using RAD Studio, the following procedure creates a modeless form dynamically.

Building this VCL application consists of the following steps:

  1. Create the project directory.
  2. Create two forms for the project.
  3. Remove the second form's invocation at startup.
  4. Link the forms.
  5. Create a control on the main form to create and display the modal form; then write the event handler.
  6. Build and run the application.

To create the two forms

  1. Choose File > New > Windows VCL Application - Delphi or File > New > Windows VCL Application - C++Builder.
  2. Choose File > New > VCL Form - Delphi or File > New > VCL Form - C++Builder accordingly.

To remove Form2's invocation at startup

  1. Choose Project > Options. The Project Options dialog displays.
  2. Select Form2 in the Auto-create forms list and click [>]. Form2 is moved to the Available forms list.
  3. Click OK to close the dialog.

To link Form1 to Form2

  1. Select Form1 and choose File > Use Unit. The Uses Unit dialog displays.
  2. Select Form2 (the form that Form1 needs to reference) in the dialog.
  3. 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

  1. Select Form1, if necessary; then, from the Standard page of the Tool Palette, place a button on the form.
  2. 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.
  3. Enter the following event handling code:
Form2 := TForm2.Create(self);
Form2 = new TForm2( this );
Note: If your application requires additional instances of the modeless form, declare a separate global variable for each instance. In most cases, you use the global reference that was created when you made the form (the variable name that matches the Name property of the form).

To build and run the application

  1. Save all files in the project; then choose Run > Run. The application executes, displaying Form1.
  2. Click the button. Form2 displays.
  3. Click Form1. Form1 becomes the active form. Form2 displays until you minimize or close it.

See Also