Recent

Author Topic: Howto: HTML Tags around Selection in Memo  (Read 5011 times)

commodianus

  • New Member
  • *
  • Posts: 18
Howto: HTML Tags around Selection in Memo
« on: March 29, 2010, 04:34:59 pm »
Explanation needed:

I have a toolbar with bold, italic, underline etc buttons on it. The aim is to format selected text with HTML tags.

e.g.

<b>some text</b>

I know I should be using SelLength and SelStart, but what do I do to actually insert the text before and after my selection?


eny

  • Hero Member
  • *****
  • Posts: 1628
Re: Howto: HTML Tags around Selection in Memo
« Reply #1 on: March 29, 2010, 05:23:16 pm »
Maybe you can work together with this one  :)

Something like this:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var s: string;
begin
  s := Memo1.Lines.Text;
  system.Insert('</b>', s, memo1.SelStart + memo1.SelLength+1);
  system.Insert('<b>',  s, memo1.SelStart+1);
  Memo1.Lines.Text := s;
end;                 
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

commodianus

  • New Member
  • *
  • Posts: 18
Re: Howto: HTML Tags around Selection in Memo
« Reply #2 on: March 29, 2010, 05:31:09 pm »
Thank you once again eny. Any Caveats you can think of?


eny

  • Hero Member
  • *****
  • Posts: 1628
Re: Howto: HTML Tags around Selection in Memo
« Reply #3 on: March 29, 2010, 08:07:28 pm »
You're welcome  8)

No real caveats here. It's a brute force solution that works.
Make sure to use ansi strings during compilation (else 's: string' is limited to 255 chars).
You could test for 'SelLength > 0', although the application doesn't crash if you don't.
It just inserts two adjacent tags (like '<i></i>').
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

 

TinyPortal © 2005-2018