Recent

Author Topic: Can I treat tmemo.caption as a string array?  (Read 2928 times)

CurtisHoffmann

  • New member
  • *
  • Posts: 8
Can I treat tmemo.caption as a string array?
« on: April 10, 2018, 08:40:25 am »
I'm just starting out with Lazarus and Free Pascal, and there's a big learning curve.
I'm trying to make a CryptoQuip type puzzle solver, where I'm using a tMemo object to hold both the encrypted text, and the user's input. Like so:

Encrypted line 1
User input (space padded)

Encrypted line 2
user input (space padded)

I'm using memo.onkeypress to trap the user input (A=B), then I want to step through encrypted lines 1 and 2, looking for the first letter entered, and then place the second letter on the next line down.

Question: Is there any way to do something like:

memo.caption[characterCounter] := ch2;

Or, do I have to make my changes to a separate variable like below all the time?

var str: string:
{change str}
memo.caption := str;

Thanks.

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Can I treat tmemo.caption as a string array?
« Reply #1 on: April 10, 2018, 01:18:45 pm »
If you want to access to the given line, you can use Lines property:

Code: Pascal  [Select][+][-]
  1. Memo.Lines[LineIndex] := 'new line value';
Code: Pascal  [Select][+][-]
  1. var
  2.   LLine: String;
  3. begin
  4.   LLine := Memo.Lines[LineIndex];

In such a simple program, it does not matter if you operate directly on properties, or if you write a text to a temporary variable. However, not everything can be done using the Lines or Caption properties - eg System.Delete will not work because it requires a variable.
« Last Edit: April 10, 2018, 01:27:33 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

CurtisHoffmann

  • New member
  • *
  • Posts: 8
Re: Can I treat tmemo.caption as a string array?
« Reply #2 on: April 10, 2018, 04:16:58 pm »
Thanks, FP! I've been trying to tackle this both ways. I think I can use .lines, I just need to sit down and play with the code a little more (but I won't have time, probably, until 2 days from now). I was hoping that trying .caption
  • as an array would be faster. But, when I tried, it was like the cursor kept jumping around all over the place, then adding a whole bunch of new characters at the beginning of the .caption string. That is, with the string "This is a test." and "T=B",  the code would try writing at string character locations 15, 25 and 28, but the result was having "B" showing up almost randomly in memo1 before repeating madly at location 0 over and over. I assume this is not the intended behavior for .caption (unless I'm doing something wrong...)

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Can I treat tmemo.caption as a string array?
« Reply #3 on: April 10, 2018, 11:37:52 pm »
Tmemo is a tricky component to do that with...

Its not good to index to a line that does not yet exists...

What I would do is drop a TEDIT on the form and a Tmemo, the Edit wouild be your entered text and the
results can be a combine of your entered text and Encrypted text in the Memo using Lines.Add;

 With the TEDIT you can create the encrypted results of your input...

 Memo1.Lines.Add( EDIT1.Caption+':'+MyEncryptedStringOfEDIT1Caption);

 Just a thought..

The only true wisdom is knowing you know nothing

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Can I treat tmemo.caption as a string array?
« Reply #4 on: April 10, 2018, 11:40:15 pm »
Thanks, FP! I've been trying to tackle this both ways.

Do what you want.

Quote
I was hoping that trying .caption as an array would be faster.

Faster (and more comfortable) will be if you write a Caption content to the temporary variable, process it and write back to Caption. You can treat the content as String (and use Caption with delimited lines) or like a TStringList (and use Lines).

Quote
But, when I tried, it was like the cursor kept jumping around all over the place, then adding a whole bunch of new characters at the beginning of the .caption string.

Hmm… If you want to remember the position of the caret, you need to write its position to temporary variable, process the text and set back the caret (old) position.

Quote
That is, with the string "This is a test." and "T=B",  the code would try writing at string character locations 15, 25 and 28, but the result was having "B" showing up almost randomly in memo1 before repeating madly at location 0 over and over. I assume this is not the intended behavior for .caption (unless I'm doing something wrong...)

Because you used property – every modification fires control update mechanism. Use local variables.
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

CurtisHoffmann

  • New member
  • *
  • Posts: 8
Re: Can I treat tmemo.caption as a string array?
« Reply #5 on: April 11, 2018, 10:02:59 am »
Thanks for all the help!
I decided to go the .lines route, saving to a string variable and doing the character manipulation that way before saving back to memo.caption.
I'm still just testing with 2-4 lines, but the screen update is so fast that I'm not worried about getting flicker right now.

 

TinyPortal © 2005-2018