RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TScreen.ActiveForm Property

Indicates which form currently has focus.

Pascal
property ActiveForm: TForm;
C++
__property TForm * ActiveForm;

Read ActiveForm to learn which form in the application has input focus. If the application is not currently active, ActiveForm is the form that will have focus when the application becomes active again. 

ActiveForm is a read-only property. To change the ActiveForm, use the SetFocus method of the form that should receive focus. ActiveForm also changes if the SetFocusedControl method of an inactive form is called to set focus to a control on that inactive form. 

After focus shifts from one form to another, the screen receives an OnActiveFormChange event.

Note: If the application window with focus is not a TForm descendant (for example, if it is a property page), it will not appear as ActiveForm. To handle such cases, use the ActiveCustomForm property instead.
 

C++ Examples: 

 

/*
This example uses a two forms with a button on the first
form. When the user clicks the button, the second form
appears. As the user switches between forms, the form that
is active is colored aqua.
Note:   You must include the unit header for form 2 to the
unit file for form 1, and add the public ColorForm method
to the definition of the TForm1 class.
*/
#include "Unit2.h"

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  Form2->Show();
}

void __fastcall TForm1::ColorForm(TObject *Sender)
{
  Color = clWhite;
  Form2->Color = clWhite;
  Screen->ActiveForm->Color = clAqua;
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  Screen->OnActiveFormChange = ColorForm;
}

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  // This stops ColorForm from being called during termination.
  Screen->OnActiveFormChange = NULL;
}

 

Delphi Examples: 

{
This example uses a two forms with a button on the first
form. When the user clicks the button, the second form
appears. As the user switches between forms, the form that
is active is colored aqua.
Note:   You must include the unit header for form 2 to the
unit file for form 1, and add the public ColorForm method
to the header for form 1’s unit.
}
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    procedure ColorForm(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses Unit2;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.Show;
end;

procedure TForm1.ColorForm(Sender: TObject);
begin
  Color := clWhite;
  Form2.Color := clWhite;
  Screen.ActiveForm.Color := clAqua;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
  Screen.OnActiveFormChange := ColorForm;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  // This stops ColorForm from being called during termination.
  Screen.OnActiveFormChange := nil;
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!