tags:

views:

51

answers:

1

Hi,

We are upgrading our application for rich text editing. In the old versions there were just plain text with dbmemo, now there should be some formatting and I want to use the TJvRichEdit for this.

The problem: The texts that already exists in the database is plaintext. When I open a mask and don't change anything in the richEdit, its still plaintext without the rtf formatting tags.

What I need: the old plain text should be automatically converted to rtf text after displaying. (I mean, I open a mask with a richEdit which displays plaintext, and at this moment the plaintext should be enhanced with the rtf tags and saved to the databse).

For this purpose I've created a descendant of the TDBRichEdit. But I have problems to finda propper place to make this happebs.

For now, I've overwritten the setBounds method with the following code:

procedure TMyRichEdit.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
  inherited;

...
    if (assigned(DataSource)) AND (assigned(DataSource.DataSet)) AND
       (self.Text <> '') AND
       (LeftStr(sDataSource.DataSet.FieldByName(DataField).AsString, 5) <> '{\rtf') then
    begin
      self.DataSource.DataSet.Edit;
      // After a call to UpdateMemo the plaintext gets extended with the rtf tags
      self.UpdateMemo;
      self.DataSource.DataSet.Post;
      self.DataSource.DataSet.Edit;
    end;
...

end;

So this works, but only if the richEdit has an anchor set. If not it doesn't work. I know thats the wrong place for this code, I've tried overwrite more propper methods but without access. How can I solve something like this? Thanks guys!

+1  A: 

You don't need to derive a new class, you can test and process the field contents in the OnAfterScroll event of the DataSet;

procedure TForm1.MyDataSet1AfterScroll(DataSet: TDataSet);
begin
  if LeftStr(DataSet.FieldByName('MyRTF').AsString, 5) <> '{\rtf') then begin
    DataSet.Edit;
    JvDBRichEdit1.UpdateMemo;
    DataSet.Post;
  end;
end;


Honestly, I don't even see the need for updating each record as soon as they're exposed. I would leave them alone until they're edited, test for the the rtf signature and update them just before posted.

Otherwise, if you'd insist on your approach, and if you decide to use TJvDBRichEdit, LoadMemo would be a good place to override. But editing there would probably cause recursion, so a user message could be posted to the owner form for notifying edit contents have been loaded. Nah, not good.. ;)

Sertac Akyuz