How to use DisplayText / DisplayFormat TStringField clientdatset Delphi

0

Hello, I want to format the fields of my table that are password, I want it to display **** instead of the password.

I tried to use the method to format values

TFloatField(dm1.q.fieldbyname('preco_prod')).DisplayFormat := 'R$ 0.00';

Only in TStringField does not have the format has DisplayText and others but none worked.

Can someone tell me how to use a text formatting correctly

    
asked by anonymous 07.04.2016 / 18:35

2 answers

1

A simple way to accomplish this is to use event onGetText of TField

The method signature is

procedure(Sender: TField; var Text: String; DisplayText: Boolean)

Then you can create a method

procedure onGetTextPasswordField(Sender: TField; var Text: String; DisplayText: Boolean);
begin
  if Displaytext then
    Text := '**********'
  else
    Text := Sender.AsString;
end;
    
26.04.2016 / 17:56
2
Hello, you have to set this mask in the visual component to be used to display the field, for example the "PasswordChar" property of a TDBEdit / TEdit, by default the default value of the field is "# 0", you must change it to "*" or any other character you want to use as a mask.

Edited:

You will need to inherit the TString List class in its own class:

  TMyGrid = class(TStringGrid)
  protected
     function CreateEditor: TInplaceEdit; override;
     procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;
  end;

And then implement the redeclared methods:

function TMyGrid.CreateEditor: TInplaceEdit;
begin
   Result := TInplaceEdit.Create(Self);
   if (Passwd) then
      TMaskEdit(Result).PasswordChar := '*';
end;

procedure TMyGrid.DrawCell(ACol, ARow: Integer; ARect: TRect;
  AState: TGridDrawState);
begin
   if (Passwd) then
      Canvas.TextRect(ARect, ARect.Left+2, ARect.Top+2, StringOfChar('*', Length(Cells[ACol, ARow])))
   else
      Canvas.TextRect(ARect, ARect.Left+2, ARect.Top+2, Cells[ACol, ARow]);
end;

Then you can use your custom TStringGrid:

   meuGrid := TMyGrid.Create(Self);
   meuGrid.Parent := Self;
   meuGrid.Left := 5;
   meuGrid.Top := 5;
   meuGrid.Width := 400;
   meuGrid.Height := 400;
   meuGrid.Options := meuGrid.Options + [goEditing];

This will work, you just need to define the logic of the variable "Passwd"

Hugs.

    
07.04.2016 / 19:11