Question

I have written a procedure that fills a RichEdit component depending on the input.

procedure LoadCPData(ResName: String);
begin
  ResName := AnsiLowercase(ResName) + '_data';
  rs := TResourceStream.Create(hInstance, ResName, RT_RCDATA);
  try
    rs.Position := 0;
    info.reMeta.Lines.LoadFromStream(rs);
  finally
    rs.Free;
  end;
end;

Note: The above procedure is stored in an external .pas file called Functions.

When I go to call the procedure in my form the RichEdit remains empty. However, if I were to place that code block in the form itself, the RichEdit component fills the data without a problem as expected. Now I could place the above code block in the form itself, but I plan on using the procedure multiple times in a case statement.

What will I need to include in order for my procedure to work?

Thank you in advanced!

Était-ce utile?

La solution

We use the TJvRichEdit control instead of TRichEdit so that we can support embedded OLE objects. This should work very similarly with TRichEdit.

procedure SetRTFData(RTFControl: TRichEdit; FileName: string);
var
  ms: TMemoryStream;
begin
  ms := TMemoryStream.Create;
  try
    ms.LoadFromFile(FileName);
    ms.Position := 0;
    RTFControl.StreamFormat := sfRichText;
    RTFControl.Lines.LoadFromStream(ms);
    ms.Clear;

    RTFControl.Invalidate;

    // Invalidate only works if the control is visible.  If it is not visible, then the
    // content won't render -- so you have to send the paint message to the control
    // yourself.  This is only needed if you want to 'save' the content after loading
    // it, which won't work unless it has been successfully rendered at least once.
    RTFControl.Perform(WM_PAINT, 0, 0);
  finally
    FreeAndNil(ms);
  end;
end;

I adapted this from another routine, so it isn't the exact same method we use. We stream the content from a database, so we don't ever read from a file. But we do write the string on to a memory stream to load it into the RTF control, so this in essence does the same thing.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top