11. Events Forms
11.1. Derivatives TCustomForm
OnActivate
This event occurs when the form is created or when changing from one form to another within the application. To detect when another application is passed to our event is used OnActivate Taplication object.
__property Classes:: TNotifyEvent OnActivate = {read = FOnActivate, write = FOnActivate, stored = ISForm};
The Following code adds to the caption of Form2 to when to list box in Form1 to Form2 is activated.
# Include "Unit1.h" / / so We Can Refer to Form1.
void __fastcall TForm2: FormActivate (TObject * Sender)
{
Form1-> ListBox1-> Items-> Add (Screen-> ActiveForm-> Caption);
}
OnClose
When you close the form OnClose event occurs. This makes a call to the OnCloseQuery to check whether it is right to close the form. If OnCloseQuery returns false, the form does not close.
enum {TCloseAction guns, caHide, caFree, caMinimize};
typedef void __fastcall (__closure * TCloseEvent) (System:: TObject * Sender, TCloseAction & Action);
__property TCloseEvent OnClose = {read = FOnClose, write = FOnClose, stored = ISForm};
The form is closed using the Close () method.
The method TcloseEvent indicates how close the form. The value of the parameters of the Action variable determines whether the form is closed.
Actions | Description |
canone | The form is not ready to be closed. |
caHide | The form is not closed, but remains hidden. The application can still access the form |
CaFree | The form closes and the occupied memory is free. |
CaMinimize | The form is minimized, before closing. This is the default action for MDI child forms. |
This example displays a message dialog box When the user Attempts to close the form. If the user clicks the Yes button, the form close; Otherwise, the form Remains open.
void __fastcall TForm1:: FormClose (TObject * Sender, TCloseAction & Action)
{
if (Application-> MessageBox ("Close application?", "Close?" MB_YESNO) == mrYes)
Action = caFree;
else
Action = fee;
}
OnCloseQuery
Occurs when the Close method is called or when the user acts on the close icon on the toolbar of the form.
typedef void __fastcall (__closure * TCloseQueryEvent) (System:: TObject * Sender, bool & CanClose)
__property TCloseQueryEvent OnCloseQuery = {read = FOnCloseQuery, write = FOnCloseQuery, stored = ISForm};
CanClose variable determines whether the form can be closed. The default is true.
For example, you can use the event OnCloseQuery to save a document in a file before closing the form.
When the user Attempts to close the form in this example, a message dialog Appears That ASKs the user if it is OK to close the form. If the user Chooses the Yes button, the form close. If the user Chooses Cancel or No, Does not close the form.
void __fastcall TForm1:: FormCloseQuery (TObject * Sender, bool & CanClose)
{
if (Application-> MessageBox ("Close the form?", "Close?" MB_YESNOCANCEL)! = mrYes)
CanClose = false;
}
OnCreate
Occurs when the form is first created. With this event will perform all necessary tasks prior to the operation of the form. Only OnCreate event occurs for each form.
__property Classes:: TNotifyEvent OnCreate = {read = FOnCreate, write = FOnCreate, stored = ISForm};
This very simple OnCreate event handler assured That the form is the Same color as the color of your Windows system application workspace:
void __fastcall TForm1:: FormCreate (TObject * Sender)
{
Color = clAppWorkSpace;
}
Note: The Color property in this example is Not preface with the name of the form. If you write the statement like this,
Form1-> Color = clAppWorkSpace;
Without the application will not run error, Form1 Because not does yet exist at the time this code is Executed
OnDeactivate
Occurs when the form loses focus. This event is used to control the transition from the currently active form to another form of the same application. When you pass our application to another, this event does not occur. To control the pace of implementation, use the event object OnDeactivate Taplication.
__property Classes:: TNotifyEvent OnDeactivate = {read = FOnDeactivate, write = FOnDeactivate, stored = ISForm};
OnDestroy
Opposite OnCreate event. Is used to free dynamically allocated memory that a form, or for cleaning.
__property Classes:: TNotifyEvent OnDestroy = {read = FOnDestroy, write = FOnDestroy, stored = ISForm};
The Following code dynamically Called BtnMyButton Create a button with the caption "My Button". The memory associated with the button is deleted When the form is Destroyed.
__fastcall TForm1:: TForm1 (TComponent * Owner)
: TForm (Owner)
{
BtnMyButton = new TButton (this);
BtnMyButton-> Parent = this;
BtnMyButton-> Caption = "My Button";
}
//------------------------------------------------ ------------------
void __fastcall TForm1:: FormDestroy (TObject * Sender)
{
delete BtnMyButton;
}
Order of events to create and destroy Form
Since the form is created and the Visible property is true, the following events occur in order of list:
Order | Events | Use |
1 | OnCreate | Preliminary tasks necessary for the operation of the form. |
2 | OnShow | Need to process the form before becoming visible. |
3 | OnActivate | When switching from one form to another within the application. |
4 | OnPaint | When the form is to be redrawn. |
Since the form is closed and releases the memory, the following events occur in order of list:
Order | Events | Use |
1 | OnCloseQuery | Save a document in a file before closing the form. |
2 | OnClose | See if it is right to close a form. |
3 | OnDeactivate | Transition is active form to another form |
4 | OnDestroy | For cleaning |
OnHelp
Occurs when the form receives a request for assistance.
typedef bool __fastcall (__closure * THelpEvent) (Word Command, int Data, bool & CallHelp)
__property Classes:: THelpEvent OnHelp = {read = FOnHelp, write = FOnHelp};
OnHide
Occurs when the form is hidden (when the Visible property set to false)
__property Classes:: TNotifyEvent OnHide = {read = FOnHide, write = FOnHide, stored = ISForm};
This example uses two forms, Each with a label and a button. When the user clicks a button, the Other form Appears and Disappears the current form. Also, a message Appears in the label of the form That is showing, Indicating That The Other form is hidden. This is the Implementation section of Unit1:
# Include "Unit2.h"
//------------------------------------------------ ------------------
__fastcall TForm1:: TForm1 (TComponent * Owner)
: TForm (Owner)
{
}
//------------------------------------------------ ------------------
void __fastcall TForm1:: Button1Click (TObject * Sender)
{
Form2-> Show ();
Hide ();
}
//------------------------------------------------ ------------------
void __fastcall TForm1:: FormHide (TObject * Sender)
{
Form2-> Label1-> Caption = "Form1 is hiding"
}
This is the Implementation section of Unit2:
# Include "Unit1.h"
//------------------------------------------------ ------------------
# Pragma resource "*. dfm"
TForm2 * Form2;
//------------------------------------------------ ------------------
__fastcall TForm2:: TForm2 (TComponent * Owner)
: TForm (Owner)
{
}
//------------------------------------------------ ------------------
void __fastcall TForm2:: Button1Click (TObject * Sender)
{
Form1-> Show ();
Hide ();
}
//------------------------------------------------ ------------------
void __fastcall TForm2: FormHide (TObject * Sender)
{
Form1-> Label1-> Caption = "Form2 is hiding"
}
OnPaint
Occurs when the form is to be redrawn. When you have to constantly display an object on a form, or as a result of resizing the form.
__property Classes:: OnPaint TNotifyEvent = {read = FOnPaint, write = FOnPaint, stored = ISForm};
The Following code loads a bitmap background onto the canvas of the main form in the OnPaint event handler. In the header file for Unit 1. to add a private member of type TForm1 Graphics:: TBitmap named TheGraphic;
void __fastcall TForm1:: FormPaint (TObject * Sender) / / OnPaint event handler
{
Form1-> Canvas-> Draw (0, 0, TheGraphic) / / Draw the graphic on the Canvas
}
void __fastcall TForm1:: FormCreate (TObject * Sender) / / OnCreate event handler
{
TheGraphic = new Graphics:: TBitmap (); / / Create the bitmap object
TheGraphic-> LoadFromFile ("C: \ APP \ BKGRND.BMP") / / Load the bitmap from a file
}
OnResize
Is activated every time you resize the form. This event is used to adjust the position of the components on the form or to redraw.
__property Classes:: TNotifyEvent OnResize = {read = FOnResize, write = FOnResize, stored = ISForm};
Keeps the Following code on the right edge Button1 Against the right edge of Form1 When Form1 is resized.
void __fastcall TForm1:: FormResize (TObject * Sender)
{
Button1-> Left = Form1-> clientWidth - Button1-> Width;
}
OnShow
Occurs just before the form is visible (when the Visible property is set to true). You can use this event to perform any process required by the form before becoming visible.
__property Classes:: TNotifyEvent OnShow = {read = FOnShow, write = FOnShow, stored = ISForm};
This example colors the form and Change When It Becomes STI visible caption:
void __fastcall TForm1:: FormShow (TObject * Sender)
{
Color = clLime;
Caption = "I am Showing"
}