如何检查 Delphi 上的 RichView 是否为空?

How to check if a RichView is empty on Delphi?

我对 Delphi 和组件 RichView 有疑问。

如何检查 RichView 是否为空?

我需要知道用户是否在 RichView 组件中输入或插入了任何内容。

谢谢!

试试这个功能,希望对你有帮助:

function TRichViewEditFrame.isEmpty: boolean;
var AStream: TMemoryStream;
begin
  Result := false;
  if TRichViewEdit(RichEditEntete).ItemCount = 0 then exit;
  AStream:= TMemoryStream.Create;
  try
    TRichViewEdit(RichEditEntete).SaveTextToStream('', AStream, 0, false, true);
    AStream.Position:= 0;
    Result:= AStream.Size = 0;
  finally
    AStream.free;
  end;
end;

谢谢大家,@BMS 的回答对我有用!

我不得不让你的函数适应我的程序,因为函数 ItemCount 对我不起作用,所以我把函数改成这个并且工作得很好:

function MyProgram.isEmpty(RView: TbsRichView): boolean;
var AStream: TMemoryStream;
begin
  AStream:= TMemoryStream.Create;
  try
    RView.Editor.SaveTextToStream('', AStream, 0, false, true);
    AStream.Position:= 0;
    Result:= AStream.Size = 0;
  finally
    AStream.free;
  end;
end;

在这个函数中我只是检查结果是否为0。如果等于0,则表示用户没有输入任何内容,或者删除了之前写入的内容。所以我可以检查 RichText 是否为空。在我的程序中,RichText 不能为空以保存表单。

您可以尝试使用GetTextLen方法:

function IsRichViewEmpty(Control: TCustomRichView): Boolean;
begin
  Result := Control.GetTextLen = 0;
end;

因为创建了TRichView handles the WM_GETTEXTLENGTH message in its own way, so I believe (haven't tested, just checked the source), that it's enough (there's the rvflCanProcessGetText flag which would prevent getting the text length, but that one is included when the TRichView控件。