Empty after html content pasted from clipboard ?

General TRichView support forum. Please post your questions here
Post Reply
Fab85
Posts: 30
Joined: Tue Apr 09, 2024 6:02 am

Empty after html content pasted from clipboard ?

Post 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 ?
Sergey Tkachenko
Site Admin
Posts: 17761
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Empty after html content pasted from clipboard ?

Post 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.
Fab85
Posts: 30
Joined: Tue Apr 09, 2024 6:02 am

Re: Empty after html content pasted from clipboard ?

Post by Fab85 »

Thank you for your source code it's work fine.
Post Reply