Clear TEdit control rad studio delphi

前端 未结 1 612
傲寒
傲寒 2021-01-23 17:14

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

Tnx all!

1条回答
  •  有刺的猬
    2021-01-23 17:58

    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;
    

    0 讨论(0)
提交回复
热议问题