Transfer contents of rich text field to another?

General TRichView support forum. Please post your questions here
Post Reply
Joe
Posts: 28
Joined: Tue Feb 14, 2012 6:15 am

Transfer contents of rich text field to another?

Post by Joe »

Hello all,

I am absolutely new to TRichView and just looking for a solution for this (Windows 7, Delphi 2010):

In a form (Form1) a record from a database is edited. It includes a rich text that is shown in in a TDBRichViewEdit field. Everything works fine.

Because there are many other fields on that form the rich text field cannot be high enough to show a lot of information (heavy scrolling). So it would be nice to open a second form (Form2) that has only a unbound rich text field of type TRichViewEdit (like a simple text editor that can be maximized to fit the whole screen), edit the text and on closing Form2 rewrite the text to the field on Form1.

I tried things like this:

rtSS : TStream is a global variable

// Test 1 ====================================:
//On click on a button in Form1 to open the text editor:
rtSS := TStream.Create;
rtSS := TStringStream.Create('', TEncoding.UTF8); // für Unicode
STATUS.rtSS.Position := 0;
Self.RichTextEdit1.SaveTextToStreamW('', rtSS, 80, false, false); // what to do here with the 80 ???
...open Form2...
...and on creating Form2:
Self.RichTextEdit1.Clear;
Self.RichTextEdit1.LoadTextFromStreamW(rtSS, 0, 0, True);
Self.RichTextEdit1.Format;
Self.RichTextEdit1.EndUpdate;

// Test 2 ====================================:
//On click on a button in Form1 to open the text editor:
rtSS := TStream.Create;
Self.RichTextEdit1.SaveRVFToStream(rtSS, false);
...open Form2...
...and on creating Form2:
Self.RichTextEdit1.BeginUpdate;
Self.RichTextEdit1.Clear;
Self.RichTextEdit1.LoadRVFFromStream(rtSS);
Self.RichTextEdit1.Format;
Self.RichTextEdit1.EndUpdate;

In either case Form2 is opened, but the rich text field is completely empty. So, how can the contents of one rich text field be taken to another (in another form)? If this is a stupid question, please be patient with me, but I am just beginning to work with TRichView.

Thank you in advance.
Joe
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

In the second case
1) create TMemoryStream instead of TStream
2) call Stream.Position := 0 before loading.

PS BeginUpdate and EndUpdate are useful when you call many methods making editing operations. LoadTextFromStreamW and LoadRVFFromStream are not editing operations, they do not update the editor (it is updated when you call Format). So BeginUpdate and EndUpdate are not needed.
Joe
Posts: 28
Joined: Tue Feb 14, 2012 6:15 am

Post by Joe »

Hello Sergey,

thank you for your response.

I did as you told, but without success - the rich text field remains empty.

To make sure that this has nothing to do with a global variable, I tried to load the rich text from Form1 to Form2, but not automatically during creating Form2. Form2 is opened and visible. On Form2 there is a button, and on clicking this button the rich text should be loaded into Form2.

But also with this scenario the rich text field on Form2 remains empty. Here is my code:

procedure TfrmRTRVBearb.Button1Click(Sender: TObject);
var
frm : TForm;
theStream : TMemoryStream;
begin

// We are in the Form2 and clicked the button <load contents from form 1>

// Get a 'pointer' to the Form1 (my own user defined function)
frm := frmGetForm('frmTest');

// On Form1 there is an example text field 'Edit1'.
// Verify, that the form can be accessed by showing the contents of field 'Edit1'
Application.messagebox(PChar(TEdit(frm.FindComponent('Edit1')).text), PChar('hello'));


theStream := TMemoryStream.Create;
theStream.Position := 0;

// Load the database-bound(!) content of Form1.RichTextEdit1 to theStream
// Does this make a difference (database-bound or not)??
TDBRichViewEdit(frm.FindComponent('RichTextEdit1')).SaveRVFToStream(theStream, false);

// Load the contents of the stream to the current Form2.RichTextEdit1 field
Self.RichTextEdit1.LoadRVFFromStream(theStream);

// theStream.Free;
end;

What goes wrong?
Best regards
Joe
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

theStream.Position := 0 must be placed between saving and loading.
After saving, the stream position is at the end of the stream. (LoadRVFFromStream loads from the stream position, so it loads nothing when the stream position is at the end)
Joe
Posts: 28
Joined: Tue Feb 14, 2012 6:15 am

Post by Joe »

Thank you, Sergey.

I did that. The program compiles and runs, but the rich text field remains empty. Now, when stepping with the debugger in the line

theStream.Position := 0;

an exception is coming up "CRVData.pas" not found. Hmmm ??

I will re-install the complete components and see what will happen.

Anyway, thank you very much for the moment...
Joe
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

If you can reproduce the problem in a simple project, please send it to richviewgmailcom.
Post Reply