How to Use Displaytext/Displayformat Tstringfield clientdatset Delphi

Asked

Viewed 1,609 times

0

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

I tried using the similar method to format values

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

Only that in Tstringfield does not have the format has Displaytext and others but none worked.

Can anyone tell me how to use a formatting for text correctly

2 answers

2

Hello, you have to set this mask on the visual component to be used to display the field, for example the "Passwordchar" property of a Tdbedit / Tedit, by default the field’s default value is "#0", you must change it to "*" or any other character you wish to use as a mask.


Edited:

You will need to inherit the Tstring List class in your 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;

and 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];

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

Hugs.

  • this in the component, but I’m wanting to format in a stringgrid

  • It’s a little complicated, but I edited the answer, take a look there...

1


One simple way to accomplish this is by using event onGetText of TField

The signature of the method 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.