Forum > Beginners

Problem setting length of TMemo text

(1/1)

new2thisagain395:
I have a form with a TMemo inside it.

Trying to programatically set the length of the text inside the memo so I can strip off some characters at the end, in this example just 1 character.


--- Code: ---SCU.Memo1.SetLength(SCU.Memo1.Text, Length(SCU.Memo1.Text) -1);

--- End code ---

Results in this error:
scu01.pas(117,22) Error: identifier idents no member "SetLength"

I believe this has to do with inheritance (TMemo doesn't have access to base class member)?

ASerge:

--- Quote from: new2thisagain395 on March 26, 2024, 07:36:12 pm ---Trying to programatically set the length of the text inside the memo so I can strip off some characters at the end, in this example just 1 character.

--- End quote ---
You should set the text one character less. If the string contains non-ASCII characters, then you need use UTF8 functions.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses LazUTF8; procedure TForm1.Button1Click(Sender: TObject);var  S: string;begin  S := Memo1.Text;   // S := UTF8Trim(S);  Memo1.Text := UTF8Copy(S, 1, UTF8LengthFast(S) - 1);end;

new2thisagain395:
Got it, thanks.

Remy Lebeau:

--- Quote from: new2thisagain395 on March 26, 2024, 07:36:12 pm ---Results in this error:
scu01.pas(117,22) Error: identifier idents no member "SetLength"

I believe this has to do with inheritance (TMemo doesn't have access to base class member)?

--- End quote ---

The error message is correct. The TMemo class does not have a SetLength() method.  SetLength() is a free-standing function in the System unit, you would have to read the TMemo.Text into a String variable first, then SetLength() that string, and then assign the string back to the TMemo.Text, eg:


--- Code: ---var S: String;
S := SCU.Memo1.Text;
SetLength(S, Length(S) - 1);
SCU.Memo1.Text := S;

--- End code ---

Another option would be to simply remove the unwanted characters directly from the TMemo by using its Sel(Start|Length|Text) properties, eg:


--- Code: ---SCU.Memo1.SelStart := SCU.Memo1.GetTextLen - 1;
SCU.Memo1.SelLength := 1;
SCU.Memo1.SelText := '';

--- End code ---

Navigation

[0] Message Index

Go to full version