Search code examples
delphifiremonkeyrad-studio

Clear TEdit control rad studio delphi


When use TEdit control on the right side stay small icon 'x'. How after click on icon clear TEdit box.

Tnx all!

enter image description here


Solution

  • Delphi provide TClearEditButton to clear the TEdit content. It can be added by right clicking and selecting AddItem - TClearEditButton from the popup menu. It also has a Click procedure overriden in FMX.Edit unit like:

    procedure TClearEditButton.Click;
    var
      EditTmp: TCustomEdit;
    begin
      inherited Click;
      EditTmp := GetEdit;
      if EditTmp <> nil then
      begin
        if EditTmp.Observers.IsObserving(TObserverMapping.EditLinkID) then
          if not TLinkObservers.EditLinkEdit(EditTmp.Observers) then
            Exit; // Can't change
        EditTmp.Text := string.Empty;
        if EditTmp.Observers.IsObserving(TObserverMapping.EditLinkID) then
          TLinkObservers.EditLinkModified(EditTmp.Observers);
        if EditTmp.Observers.IsObserving(TObserverMapping.ControlValueID) then
          TLinkObservers.ControlValueModified(EditTmp.Observers);
      end;
    end;
    

    Which make you don't need to write OnClick event handler for the TClearEditButton unless you want to do some other job along side with clearing the edit.

    If you are using a TEditButton then you should write the OnClick event handler like:

    procedure TForm1.EditButton1Click(Sender: TObject);
    begin
      Edit1.Text:= EmptyStr;
    end;