Page 1 of 1

[Example] How to display a placeholder text for empty editor

Posted: Sun Oct 13, 2019 6:55 am
by Sergey Tkachenko
There is no built-in property for displaying a text hint when TRichViewEdit is empty (we are considering to add it in future updates).
But you can implement it using OnPaint event:

Code: Select all

procedure TForm3.RichViewEdit1Paint(Sender: TCustomRichView; ACanvas: TCanvas;
  Prepaint: Boolean);
var
  X, Y: Integer;
begin
  if not PrePaint and (Sender.ItemCount = 1) and (Sender.GetItemStyle(0) >= 0) and
    (Sender.GetItemText(0) = '') then
  begin
    Sender.GetItemClientCoords(0, X, Y);
    ACanvas.Font.Name := 'Tahoma';
    ACanvas.Font.Charset := DEFAULT_CHARSET;
    ACanvas.Font.Color := clGrayText;
    ACanvas.Font.Style := [];
    ACanvas.Font.Size := Sender.Style.TextStyles[Sender.GetItemStyle(0)].Size;
    ACanvas.Brush.Style := bsClear;
    SetTextAlign(ACanvas.Handle, TA_LEFT or TA_TOP);
    ACanvas.TextOut(X, Y, 'Please write something...');
  end;
end;

Re: [Example] How to display a placeholder text for empty editor

Posted: Mon Apr 15, 2024 3:44 pm
by jonjon
Has this been implemented yet ? Having the ability to define a placeholder RVF file would be amazing!
Also, this doesn't work for a blank TRichView as ItemCount = 0

Re: [Example] How to display a placeholder text for empty editor

Posted: Wed Apr 24, 2024 6:05 pm
by Sergey Tkachenko
Sorry, it is still not implemented as a component property.

To work with empty TRichView, the first condition must be changed to:

Code: Select all

if not PrePaint and 
  ((Sender.ItemCount = 0) or
   ((Sender.ItemCount = 1) and (Sender.GetItemStyle(0) >= 0) and (Sender.GetItemText(0) = ''))) then
The original condition (assuming an empty text item) did not included this check, because a placeholder text usually make sense in editors, not in viewers.

Re: [Example] How to display a placeholder text for empty editor

Posted: Thu Apr 25, 2024 8:12 am
by jonjon
OK thanks. I hope that you'll consider adding it for both viewers and editors in the future. A placeholder also makes sense in a viewer when it is waiting from data from a DB or a remote service... to avoid displaying an empty blank content to the end-user.