Recent

Author Topic: Appending text to a TMemo  (Read 29852 times)

tech-pro

  • Full Member
  • ***
  • Posts: 173
    • Tech-Pro.net
Appending text to a TMemo
« on: November 10, 2007, 09:57:30 am »
I am using a TMemo to display text that is received from a serial port. It appears that when I add newly received text, like this:

Memo.Text := Memo.Text + newtext;

a newline gets added to the end of the text, so the cursor is on the next line and every chunk of text received is on a new line.

Any ideas for a solution?
Julian

Bart

  • Hero Member
  • *****
  • Posts: 3546
    • Bart en Mariska's Webstek
RE: Appending text to a TMemo
« Reply #1 on: November 10, 2007, 12:33:56 pm »
I don't know how other Delphi versions behave, but D3 Pro (Win9x) does not add the newline as opposed to Lazarus.

If this Lazarus behaviour also differs from newer Delphi versions, it may be considered a bug and reported IMHO.

Bart

antonio

  • Hero Member
  • *****
  • Posts: 605
RE: Appending text to a TMemo
« Reply #2 on: November 10, 2007, 12:38:13 pm »
Try
memo1.lines.text := trim(memo1.lines.text) + 'text';

tech-pro

  • Full Member
  • ***
  • Posts: 173
    • Tech-Pro.net
RE: Appending text to a TMemo
« Reply #3 on: November 10, 2007, 01:14:36 pm »
I just checked in Delphi 6 and it does not add the new line.

I tried your suggestion, Antonio, and although it does partially solve the problem (except that it removes any trailing spaces, but that could be fixed) the trouble is that the new line is added when the text is written to the control, so the cursor is positioned on the next line. This is a problem because I want it to be like a real terminal window where you can type text as well as have received text printed up.

The original Delphi code didn't use a TMemo. It derived from TCustomEdit and made it multi-line with word-wrap and a scroll bar. Unfortunately the method used to achieve this - by recreating the window after adding some styles - doesn't seem to work with the LCL and despite much studying of the LCL source code I haven't figured out how to do this.
Julian

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
RE: Appending text to a TMemo
« Reply #4 on: November 10, 2007, 01:39:55 pm »
I think the bug already has been reported.

antonio

  • Hero Member
  • *****
  • Posts: 605
RE: Appending text to a TMemo
« Reply #5 on: November 10, 2007, 03:28:44 pm »
Should the application run on Windows only?

Bart

  • Hero Member
  • *****
  • Posts: 3546
    • Bart en Mariska's Webstek
Re: RE: Appending text to a TMemo
« Reply #6 on: November 10, 2007, 04:06:39 pm »
Quote from: "Vincent"
I think the bug already has been reported.

Yeah, it's in bugreport #9653.

It seems that TStrings.SetTextStr adds a LineEnding after the text becaus it uses TSrings.Add, which uses TSrings.Insert, which in fact adds the LineEnding to the string.

Bart

tech-pro

  • Full Member
  • ***
  • Posts: 173
    • Tech-Pro.net
RE: Re: RE: Appending text to a TMemo
« Reply #7 on: November 10, 2007, 08:51:23 pm »
It's going to run on Windows though I wanted to make as little of it Windows-specific as possible.

I checked Delphi adding the text to TStrings.Text and it appeared to add a line ending, the same as Lazarus. Only the memo does it differently.

I have managed to work around it after a fashion using something similar to Antonio's first suggestion.
Julian

Bart

  • Hero Member
  • *****
  • Posts: 3546
    • Bart en Mariska's Webstek
RE: Re: RE: Appending text to a TMemo
« Reply #8 on: November 11, 2007, 11:57:45 am »
You might want to consider using SelText for this purpose? It puts the text at the position of the cursor (which might be a problem if the user changes that).

Bart

tech-pro

  • Full Member
  • ***
  • Posts: 173
    • Tech-Pro.net
RE: Re: RE: Appending text to a TMemo
« Reply #9 on: November 11, 2007, 02:24:02 pm »
Yes, I will try to do something like that. As this has been reported as a bug, it will hopefully eventually be fixed.
Julian

tech-pro

  • Full Member
  • ***
  • Posts: 173
    • Tech-Pro.net
RE: Re: RE: Appending text to a TMemo
« Reply #10 on: November 13, 2007, 03:15:30 pm »
Further to this, I've managed to work around the problem so the text can be added a character at a time, but has anyone any suggestions as to how I can avoid flicker when the memo is updated? I've tried disabling it before the update and enabling it afterwards, but that hasn't made any difference.
Julian

Bart

  • Hero Member
  • *****
  • Posts: 3546
    • Bart en Mariska's Webstek
RE: Re: RE: Appending text to a TMemo
« Reply #11 on: November 13, 2007, 06:01:00 pm »
TMemo.Lines.Beginupdate / Endupdate?
Make your form doublebuffered?
(I only know how to do that in win32) Call windows to lock the the painting of the form entirely?

Just curious: how do you add one character at a time, without using the text property I assume (since this will add the LineEnding).

Does anyone know how to remove in code the last LineEmding from the memo?

Bart

tech-pro

  • Full Member
  • ***
  • Posts: 173
    • Tech-Pro.net
RE: Re: RE: Appending text to a TMemo
« Reply #12 on: November 13, 2007, 07:31:37 pm »
I'll investigate if those possibilities exist in LCL. Thanks.

At the moment, there's no alternative than to add characters using the Text property. Dealing with the line end is very kludgey. You have to remove CR and LF characters from the end of the string, unless you actually added them yourself and therefore want them.
Julian

Leledumbo

  • Hero Member
  • *****
  • Posts: 8114
  • Programming + Glam Metal + Tae Kwon Do = Me
RE: Re: RE: Appending text to a TMemo
« Reply #13 on: November 14, 2007, 05:44:24 am »
Quote
Does anyone know how to remove in code the last LineEnding from the memo?

Use Trim;
Code: [Select]
function Trim(const S: widestring): widestring;

tech-pro

  • Full Member
  • ***
  • Posts: 173
    • Tech-Pro.net
RE: Re: RE: Appending text to a TMemo
« Reply #14 on: November 14, 2007, 11:05:52 am »
Unfortunately sometimes you want new lines to appear in the memo text, also Trim will remove spaces (remember, you are adding text character by character.)

You have to store what you added and manually remove line endings only if they were not added by you.
Julian