RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TLabeledEdit.OnExit Event

Occurs when the input focus shifts away from one control to another.

Pascal
property OnExit: TNotifyEvent;
C++
__property TNotifyEvent OnExit;

Use the OnExit event handler to provide special processing when the control ceases to be active.  

The OnExit event does not occur when switching between forms or between another application and your application. 

When switching between controls in separate container controls such as the TPanel and the TGroupBox controls, an OnExit event occurs for the control inside the container before the OnExit event of the container. 

Similarly, an OnEnter event of the container occurs before the OnEnter event of the control in a container when focus moves to a control inside a container. 

For example, consider a form with an OK button and a group box that contains three radio buttons, where focus is currently on the OK button. When the user clicks one of the radio buttons, an OnExit event of the button occurs, followed by an OnEnter event on the group box, and finally an OnEnter event on the radio button that was clicked. If the user then clicks on the OK button, an OnExit event for the radio button occurs followed by an OnExit event for the group box, and then the button's OnEnter event occurs.

Note: In some control classes the ActiveControl property updates before the OnExit event occurs.
 

C++ Examples: 

 

/*
This example uses three mask edit components on a form,
where the third mask edit box is set to be read only.
When the application runs, it initializes the mask edit boxes
to contain the empty string, to use the Courier font and to
use a particular mask, corresponding to IP address coding convetions.
The mask edit boxes display the IP address, the subnet mask
and the subnet address correspondingly. When the user exits the
subnet mask edit box, the subnet address is automatically calculated
using bit-wise AND operations and displayed in the third mask edit box.
An additional test is made to assure that the values in the IP address
and in the subnet mask are in the interval from 0 to 255 ,
as this validation test cannot be accomplished
using the edit mask property alone.
*/
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  // initialize the maskedit boxes to contain the empty string
  MaskEdit1->Text = "";
  MaskEdit2->Text = "";
  MaskEdit3->Text = "";

  // use the Courier font in the maskedit boxes
  MaskEdit1->Font->Name = "Courier";
  MaskEdit2->Font->Name = "Courier";
  MaskEdit3->Font->Name = "Courier";

  // initialize the mask of each maskedit box
  // according to IP address coding conventions
  MaskEdit1->EditMask = "!099.099.099.099;1; ";
  MaskEdit2->EditMask = "!099.099.099.099;1; ";
  MaskEdit3->EditMask = "!099.099.099.099;1; ";

  MaskEdit2->OnExit = MaskEdit2Exit;
}

void __fastcall TForm1::MaskEdit2Exit(TObject *Sender)
{
  // extract the net and host address from the IP
  String IP = MaskEdit1->Text;
  int net1 = IP.SubString(0, 3).TrimRight().ToInt();
  int net2 = IP.SubString(5, 3).TrimRight().ToInt();
  int host1 = IP.SubString(9, 3).TrimRight().ToInt();
  int host2 = IP.SubString(13, 3).TrimRight().ToInt();

  // a range test that you cannot validate through edit masks
  if (net1 < 0 || net1 > 255 ||
      net2 < 0 || net2 > 255 ||
      host1 < 0 || host1 > 255 ||
      host2 < 0 || host2 > 255)
    throw(Exception("Not a valid IP address."));

  // extract the net and host mask from the subnet mask
  String mask = MaskEdit2->Text;
  int netmask1 = mask.SubString(0, 3).TrimRight().ToInt();
  int netmask2 = mask.SubString(5, 3).TrimRight().ToInt();
  int hostmask1 = mask.SubString(9, 3).TrimRight().ToInt();
  int hostmask2 = mask.SubString(13, 3).TrimRight().ToInt();

  // a range test that you cannot validate through edit masks
  if (netmask1 < 0 || netmask1 > 255 ||
      netmask2 < 0 || netmask2 > 255 ||
      hostmask1 < 0 || hostmask1 > 255 ||
      hostmask2 < 0 || hostmask2 > 255)
    throw(Exception("Not a valid subnet mask."));

  // compute the subnet address using bit-wise AND
  int sub_net1 = net1 & netmask1;
  int sub_net2 = net2 & netmask2;
  int sub_host1 = host1 & hostmask1;
  int sub_host2 = host2 & hostmask2;

  // display the subnet address
  MaskEdit3->Text =
    String(sub_net1) + "." + String(sub_net2) + "." +
    String(sub_host1) + "." + String(sub_host2);
}
/*
This example uses an edit box and a memo control on a form.
When either the edit box or the memo is the active control,
it is colored yellow. When the active control becomes 
inactive, the color of the control returns to the Windows
system color for a window.  These event handlers could also
be shared.
*/
void __fastcall TForm1::Edit1Enter(TObject *Sender)
{
  Edit1->Color = clYellow;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1Exit(TObject *Sender)
{
  Edit1->Color = clWindow;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Memo1Enter(TObject *Sender)
{
  Memo1->Color = clYellow;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Memo1Exit(TObject *Sender)
{
  Memo1->Color = clWindow;
}

 

Delphi Examples: 

{
This example uses three mask edit components on a form,
where the third mask edit box is set to be read only.
When the application runs, it initializes the mask edit boxes
to contain the empty string, to use the Courier font and to
use a particular mask, corresponding to IP address coding convetions.
The mask edit boxes display the IP address, the subnet mask
and the subnet address correspondingly. When the user exits the
subnet mask edit box, the subnet address is automatically calculated
using bit-wise AND operations and displayed in the third mask edit box.
An additional test is made to assure that the values in the IP address
and in the subnet mask are in the interval from 0 to 255,
as this validation test cannot be accomplished
using the edit mask property alone.
}
procedure TForm1.FormCreate(Sender: TObject);
begin
  { initialize the maskedit boxes to contain the empty string }
  MaskEdit1.Text := '';
  MaskEdit2.Text := '';
  MaskEdit3.Text := '';

  { use the Courier font in the maskedit boxes }
  MaskEdit1.Font.Name := 'Courier';
  MaskEdit2.Font.Name := 'Courier';
  MaskEdit3.Font.Name := 'Courier';

  {
  initialize the mask of each maskedit box
  according to IP address coding conventions
  }
  MaskEdit1.EditMask := '!099.099.099.099;1; ';
  MaskEdit2.EditMask := '!099.099.099.099;1; ';
  MaskEdit3.EditMask := '!099.099.099.099;1; ';

  MaskEdit2.OnExit := MaskEdit2Exit;
end;

procedure TForm1.MaskEdit2Exit(Sender: TObject);
var
  net1, net2, host1, host2,
  netmask1, netmask2, hostmask1, hostmask2,
  sub_net1, sub_net2, sub_host1, sub_host2: Integer;

  IP, mask: String;

begin
  // extract the net and host address from the IP
  IP := MaskEdit1.Text;
  net1 := StrToInt(TrimRight(Copy(IP, 0, 3)));
  net2 := StrToInt(TrimRight(Copy(IP, 5, 3)));
  host1 := StrToInt(TrimRight(Copy(IP, 9, 3)));
  host2 := StrToInt(TrimRight(Copy(IP, 13, 3)));

  // a range test that you cannot validate through edit masks
  if ((net1 < 0) Or (net1 > 255) Or
      (net2 < 0) Or (net2 > 255) Or
      (host1 < 0) Or (host1 > 255) Or
      (host2 < 0) Or (host2 > 255)) then
    raise EArgumentException.Create('Not a valid IP address.');

  // extract the net and host mask from the subnet mask
  mask := MaskEdit2.Text;
  netmask1 := StrToInt(TrimRight(Copy(mask, 0, 3)));
  netmask2 := StrToInt(TrimRight(Copy(mask, 5, 3)));
  hostmask1 := StrToInt(TrimRight(Copy(mask, 9, 3)));
  hostmask2 := StrToInt(TrimRight(Copy(mask, 13, 3)));

  // a range test that you cannot validate through edit masks
  if ((netmask1 < 0) Or (netmask1 > 255) Or
      (netmask2 < 0) Or (netmask2 > 255) Or
      (hostmask1 < 0) Or (hostmask1 > 255) Or
      (hostmask2 < 0) Or (hostmask2 > 255)) then
    raise EArgumentException.Create('Not a valid subnet mask.');

  // compute the subnet address using bit-wise AND
  sub_net1 := net1 And netmask1;
  sub_net2 := net2 And netmask2;
  sub_host1 := host1 And hostmask1;
  sub_host2 := host2 And hostmask2;

  // display the subnet address
  MaskEdit3.Text :=
    IntToStr(sub_net1) + '.' + IntToStr(sub_net2) + '.' +
    IntToStr(sub_host1) + '.' + IntToStr(sub_host2);
end;
{
This example uses an edit box and a memo control on a form.
When either the edit box or the memo is the active control,
it is colored yellow. When the active control becomes 
inactive, the color of the control returns to the Windows
system color for a window.  These event handlers could also
be shared.
} 
procedure TForm1.Edit1Enter(Sender: TObject);
begin
  Edit1.Color := clYellow;
end;

procedure TForm1.Edit1Exit(Sender: TObject);
begin
  Edit1.Color := clWindow;
end;

procedure TForm1.Memo1Enter(Sender: TObject);
begin
  Memo1.Color := clYellow;
end;

procedure TForm1.Memo1Exit(Sender: TObject);
begin
  Memo1.Color := clWindow;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!