Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem assigning string to Memo variable

406 views
Skip to first unread message

Tristan Martinez

unread,
May 19, 1999, 3:00:00 AM5/19/99
to
I'm having some trouble assigning a string to a Memo variable.

AMemo is a user-defined TMemo variable. I am attempting to assign a string
to the AMemo variable and then using the AMemo.Lines.Count to create a text
box large enough to contain the string.

Unfortunately, I always get the value 0 for AMemo.Lines.Count and the string
is never wrapped within the boundaries (I have the WordWrap property set to
true and the Width set to a value smaller than the lenght of the string).

If anyone can suggest a way to display a string that can be word-wrapped in
a predefined width and allow me to obtain the number of lines required, I
would greatly appreciate it.

Thanks in advance.

Tristan

Giannis kozikopulos

unread,
May 20, 1999, 3:00:00 AM5/20/99
to Tristan Martinez
Hi Tristan,

I don't know why but you can find out the maximum line number will ever
used by the following calculations.

maxlineno:=(Stringlength div maxlength) +1;

Hope this helps

Best Regards
Johnnie.

Peter Below (TeamB)

unread,
May 20, 1999, 3:00:00 AM5/20/99
to
> I'm having some trouble assigning a string to a Memo variable.

Let's get the nomenclature straight here: a memo is an object and you cannot
assign a string to an object variable. You can assign it to the Text property
of the memo, however, which is of type String.

> If anyone can suggest a way to display a string that can be word-wrapped in
> a predefined width and allow me to obtain the number of lines required, I
> would greatly appreciate it.

There is a way to "measure" a string for display using the API function
DrawText.

Var
r: TRect;
S: String;
Begin
S:= .....; // string to measure
r:= Rect( 0, 0, 250, 10 ); // allow 250 pixels width
canvas.font := somefont; // define font to use
DrawText(canvas.handle,
Pchar(S), Length(S), r,
dt_calcrect or dt_wordbreak or dt_left );
// r.bottom now contains the required height for the text

Note that this does not actually count lines but
r.bottom / Abs(somefont.height) should get you a good approximation for that.

Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!


Tristan Martinez

unread,
May 21, 1999, 3:00:00 AM5/21/99
to
>> If anyone can suggest a way to display a string that can be word-wrapped
in
>> a predefined width and allow me to obtain the number of lines required, I
>> would greatly appreciate it.
>There is a way to "measure" a string for display using the API function
>DrawText.


Isn't it a feature of a TMemo object that allows a string to be assigned to
its text property and have that string Word Wrapped within the predefined
Width? This is where I am having difficulties.

When I perform this:

AMemo.Text := myString;

And then I try to find this:

LineCount := AMemo.Lines.Count;

I get the value 0.

When I try to reference the first line in AMemo.Text by doing:

AMemo.Lines[0];

I get a reference error. I wish some method that will allow me to word wrap
a string within a predefined width. I thought TMemo would let me do this,
am I wrong?

Thanks in advance.

Tristan

Rudy Velthuis

unread,
May 21, 1999, 3:00:00 AM5/21/99
to
In article <7i4g25$jp...@forums.borland.com>, Tristan Martinez wrote...

>When I perform this:
>
> AMemo.Text := myString;
>
>And then I try to find this:
>
> LineCount := AMemo.Lines.Count;
>
>I get the value 0.
>
>When I try to reference the first line in AMemo.Text by doing:
>
> AMemo.Lines[0];
>
>I get a reference error. I wish some method that will allow me to word wrap
>a string within a predefined width. I thought TMemo would let me do this,
>am I wrong?

Are you sure myString <> '' ? Otherwise, you would have an empty memo, so
Lines[0] would be invalid (which would match the fact that Lines.Count =
0).

If you want to display a word wrapped string, try TLabel also. And have a
look at the Win32 function DrawText, like Peter said. This has a lot of
options to draw text, including word wrap.

--
Rudy Velthuis
rvel...@gmx.net
http://members.xoom.com/RVelthuis

Rudy Velthuis

unread,
May 21, 1999, 3:00:00 AM5/21/99
to
In article <7i4g25$jp...@forums.borland.com>, Tristan Martinez wrote...

>Isn't it a feature of a TMemo object that allows a string to be assigned to


>its text property and have that string Word Wrapped within the predefined
>Width? This is where I am having difficulties.

Ok, I've done this:

Select New Application from the File menu.

In the form, drop a memo and leave it exactly as it is. Now it propably
contains the text Memo1. Also drop a Label component on it (Label1).

Doubleclick on the form (not on the memo or the label!) and enter this in
the FormCreate procedure in the editor:

procedure TForm1.FormCreate(Sender: TObject);
var
MyString: string;
begin
MyString := 'This is the first line of text, it must be a ' +
'bit longer than just a few characters.';
Memo1.Text := MyString;
Label1.Caption := IntToStr(Memo1.Lines.Count);
end;

Now press run and see what happens: the memo is word wrapped and the
label shows the number 2.

If this doesn't work, you have a serious problem. Try to re-install
Delphi 4 and apply D4 SP2 and D4 SP3 (or whatever version you use: D3,
D2).
--
Rudy Velthuis

0 new messages