Clear TEdit control rad studio delphi

2020-04-24 17:08发布

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

1条回答
我命由我不由天
2楼-- · 2020-04-24 17:16

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;
查看更多
登录 后发表回答