Board index » delphi » Need to programmatically edit memo contents

Need to programmatically edit memo contents

I'm using a Paradox database and I'm trying to do a search and replace algorithm
on a memo field from a database.  For example, I want to replace "{Name}" with
the contents of a field from another table.  I'm in serious need of help.  
Here is what I've tried:
*  Using the Lines property of the control so that I could work with strings.  
   This seems to insert unwanted line breaks in the text.
*  Converting the field to a PChar.  I didn't get anywhere with this one.
*  Using the SaveToStream method.  I keep getting GPFs.

What can I do?

 

Re:Need to programmatically edit memo contents


Quote
pr...@chattanooga.net wrote:
>I'm using a Paradox database and I'm trying to do a search and replace algorithm
>on a memo field from a database.  For example, I want to replace "{Name}" with
>the contents of a field from another table.  I'm in serious need of help.  
>Here is what I've tried:
>*  Using the Lines property of the control so that I could work with strings.  
>   This seems to insert unwanted line breaks in the text.
>*  Converting the field to a PChar.  I didn't get anywhere with this one.
>*  Using the SaveToStream method.  I keep getting GPFs.

>What can I do?

I had an unrelated but similar problem accessing the text in a (normal) TMemo.
(I would think I was storing the text as it existed in the memo, but what I
was really storing was the text the way it was currently wrapping in the memo -
very irritating indeed)

I ended up doing

   BufSize := MyMemo.GetTextLen + 1;
   pcMyMemoText := StrAlloc(BufSize);
   MyMemo.GetTextBuf(pcMyMemoText, BufSize);

At this point, pcMyMemoText is a PChar to a fresh copy of the memo field.  
When you've done you're stuff to that copy, you can do

   MyMemo.SetTextBuf(pcMyMemoText);

Note:  this code *looks* fine to me and is working fine for me so far, but
has not been through extensive testing of any sort and I cannot vouch for
it as I'm learning this stuff as I go along.

cps

Re:Need to programmatically edit memo contents


Quote
pr...@chattanooga.net wrote:

: I'm using a Paradox database and I'm trying to do a search and replace algorithm
: on a memo field from a database.  For example, I want to replace "{Name}" with
: the contents of a field from another table.  I'm in serious need of help.  
: Here is what I've tried:
: *  Using the Lines property of the control so that I could work with strings.  
:    This seems to insert unwanted line breaks in the text.
: *  Converting the field to a PChar.  I didn't get anywhere with this one.
: *  Using the SaveToStream method.  I keep getting GPFs.

You will want to use a TBlobStream object. This object is predicated
around programmatic access, reading from or writing to, memo fields. Use
of the TBlobStream object does not require use of any memo component.

The TBlobStream object allows you to access the contents of a memo field
for the current record. You have the choice of read-onyl, write-only,
or read-write access (parameter in the Create method). The Size method
returns the size of the current record's memo contents. The Seek method
allows you to set the current position within the TBlobStream object, and
the Read and Write methods allow you to read from and write to the memo
field via the TBlobStream object.

For example:

  var
    BS: TBlobStream;
    InsertWord: array [0..255] of Char;
  begin
    BS := TBlobStream.Create(Table1Notes, bmWrite);
    try
      StrPCopy(InsertWord, Edit1.Text);
      BS.Write(InsertWord, 255);
    finally
      BS.Free;
    end;
  end;

Both the Read and Write methods require use of a PChar type variable as
the destination of a Read or the source for a Write. If you use the Read
method to fill a PChar buffer, you can then parse that buffer for your
searching.

--
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/ Steve Koterski               _/   The opinions expressed here are    _/
_/ koter...@borland.com         _/         exclusively my own           _/
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

Other Threads