Page 1 of 1

Empty after html content pasted from clipboard ?

Posted: Fri Apr 04, 2025 2:10 pm
by Fab85
Hello,

To test is a TRichViewEdit is empty.
Equivalent of a :

Code: Select all

if (trim(MyTRichEdit.Lines.text)='') then ...
I use this function :

Code: Select all

// look for an equivalent to 
function IsRichViewEmpty(ARichView: TCustomRichView): Boolean;
var
  LIndex: Integer;
begin
  Result := True;
  try
      for LIndex := 0 to ARichView.ItemCount - 1 do
      begin
        if (ARichView.GetItemText(LIndex) <> '')   then
        begin
          Result := False;
          Exit;
        end;
      end;
  except

  end;
end;

It's the good way to do or not ?

Because sometime if the end-user copy an HTML content, select all content of the TRichViewEdit destination , and paste the HTML content (Using CTRL + V or popup with native action), my function seems to return true ?
Something is missing like a validation / post before check if it is empty ?

Re: Empty after html content pasted from clipboard ?

Posted: Fri Apr 04, 2025 5:20 pm
by Sergey Tkachenko
Your code will returns True if ARichView contains multiple lines (because it does not check the item count) or if it contains an image without an assigned name (because it does not check GetItemSyle)

Use

Code: Select all

function IsRichViewEmpty(ARichView: TCustomRichView): Boolean;
begin
  Result := (ARichView.ItemCount = 0) or
   ((ARichView.ItemCount = 1) and (ARichView.GetIemStyle(0) >= 0) and (ARichView.GetIemText(0) = ''));
end;
This function returns True of ARichView does not have items, or it has a single empty text item.

Re: Empty after html content pasted from clipboard ?

Posted: Mon Apr 07, 2025 8:03 am
by Fab85
Thank you for your source code it's work fine.