Board index » delphi » TMemo Field without a TMemo Component

TMemo Field without a TMemo Component

Does anyone know how to access individual lines stored in a TMemo field
without using a TMemo component ?

I want to display a specific line from a TMemo field in a TEdit
component, change it, then write it back to the TMemo field in the
correct line position.

I know how to use 'assign' to transfer text from a TMemo component to a
TMemo field, but surely there is a way of getting at these strings and
displaying them without a TMemo component.

Any help will be greatly appreciated.

Regards,

Sally Brown
s...@rb.icl.co.uk

 

Re:TMemo Field without a TMemo Component


Quote
Sally Brown (s...@rb.icl.co.uk) wrote:

: Does anyone know how to access individual lines stored in a TMemo field
: without using a TMemo component ?
This is not that trivial. First if you have long lines in the memo
the tmemo will do a word wrap (unless you switched it off), doing this by
hand is a rather hard task. The second problem is the worse - you can only
get a stream of data from a tmemofield, so you'd have to read in a number of
bytes from the memo, check if a line ended and then either continue reading
in bytes or use the data till the first line ending. If you even want to
modify single lines within you'd have to read in the whole memo and then
write back the data till you modified line, your modified line and then the
rest. To do all this you'd need to use a TBlobStream (don't be mislead by
the online help according the getdata/setdata methods, they don't work).
If you have the VCL source you can of course check how Borland has done
this, e.g. in the TMemo.LoadFromStream method.

: I know how to use 'assign' to transfer text from a TMemo component to a
: TMemo field, but surely there is a way of getting at these strings and
: displaying them without a TMemo component.
Why not simply make the tmemo invisible and then work with the lines from
it, that is much easier than the above.

Hope this helps,
     Andy

--
----------------------------------------------------------------------------
Andreas H"orstemeier                       | "Wenn ist das Nunstueck git
email: hoer...@hal1.physik.uni-dortmund.de | und Slotermeyer? Ja! Beiherhund
       a...@farpoint.sauerland.de          | das Oder die Flipperwaldt
fido:     2:2444/4505.3                    | gersput."
astronet: 122:490/1.53                     |                  (Monty Python)
----------------------------------------------------------------------------  

Re:TMemo Field without a TMemo Component


In article <4gfdkd$...@nx2.hrz.uni-dortmund.de>,
hoer...@hal1.physik.uni-dortmund.de says...

Quote

>Sally Brown (s...@rb.icl.co.uk) wrote:
>: Does anyone know how to access individual lines stored in a TMemo field
>: without using a TMemo component ?
>This is not that trivial. First if you have long lines in the memo
>the tmemo will do a word wrap (unless you switched it off), doing this by
>hand is a rather hard task. The second problem is the worse - you can only
>get a stream of data from a tmemofield, so you'd have to read in a number of
>bytes from the memo, check if a line ended and then either continue reading
>in bytes or use the data till the first line ending. If you even want to
>modify single lines within you'd have to read in the whole memo and then
>write back the data till you modified line, your modified line and then the
>rest. To do all this you'd need to use a TBlobStream (don't be mislead by
>the online help according the getdata/setdata methods, they don't work).
>If you have the VCL source you can of course check how Borland has done
>this, e.g. in the TMemo.LoadFromStream method.

>: I know how to use 'assign' to transfer text from a TMemo component to a
>: TMemo field, but surely there is a way of getting at these strings and
>: displaying them without a TMemo component.
>Why not simply make the tmemo invisible and then work with the lines from
>it, that is much easier than the above.

>Hope this helps,

I've recently had the same need. Here's my solution.

function TMDI4Inherit.Memo2String(TMF: TMemoField): String;
var
    MemStream: TMemoryStream;
    Buffer: PChar;
    BufSize: Integer;
    S: String;
Begin
    MemStream := TMemoryStream.Create;
    TMF.SaveToStream(MemStream);
    BufSize := MemStream.Size;
    GetMem(Buffer, BufSize+1);
    MemStream.Position := 0;
    MemStream.Read(Buffer[0], BufSize);
    Buffer[BufSize] := #0;
    S := StrPas(Buffer);
    FreeMem(Buffer, BufSize+1);
    Memtream.Free;
    Memo2String := S;
End;

It works well, and will give you the full text (multiple lines included) from any
tmemofield object you send it.

Good luck.

btw: If anyone knows of a better way to do this I'd be interested in seeing it.
--
Brad Mettee
PC HotShots, Inc.
Baltimore, MD
TAFG, STTNG & TOM

Re:TMemo Field without a TMemo Component


Quote
Brad Mettee (bmet...@clark.net) wrote:

: In article <4gfdkd$...@nx2.hrz.uni-dortmund.de>,
: hoer...@hal1.physik.uni-dortmund.de says...
: >
: >Sally Brown (s...@rb.icl.co.uk) wrote:
: >: Does anyone know how to access individual lines stored in a TMemo field
: >: without using a TMemo component ?
[..]
:
: I've recently had the same need. Here's my solution.
:
: function TMDI4Inherit.Memo2String(TMF: TMemoField): String;
[..]
: End;
:
: It works well, and will give you the full text (multiple lines included)
: from any tmemofield object you send it.
But that code you posted only reads in the whole memo text into a pchar and
then copies the first 255 chars into a string; that doesn't answer Sally's
question on how to access single lines within the memo.

Bye,
   Andy

--
----------------------------------------------------------------------------
Andreas H"orstemeier                       | "If Saddam Hussein had said he
                                           | was doing a school project on
email: hoer...@hal1.physik.uni-dortmund.de | Kuweit, he'd have found life a
       a...@farpoint.sauerland.de          | lot easier..."
fido:      2:2444/4505.3                   |     (Terry Pratchett, 'Johnny
astronet:  122:490/1.53                    |                  and the dead')  
----------------------------------------------------------------------------

Other Threads