How to Clear Multiple Edits Without Refering to Them One by One

Let’s say you have a data entry form or a MP3 ID3 tag editor form. The user clicks the “Clear” button. The more edit controls you have the less you want to write the code to clear them :-) Well, here’s a better idea:

procedure TMainForm.btnClear(Sender: TObject);
var
  iI: Integer;
begin
  for iI := 0 to ComponentCount - 1 do
    if (Components[iI] is TEdit) then
      TEdit(Components[iI]).Clear;
end;

If you want to exclude a edit control from being cleared use this:

!!Note: The name check must be after the VCL class check.

if (Components[iI] is TEdit) and (TEdit(Components[iI].Name <> 'edDontClear')) then
  TEdit(Components[iI]).Text := '';

Tags: , , , , , , , , , , , ,

No Comments Yet

You can be the first to comment!

Leave a comment

OpenID Login

Standard Login