Lazarus

Programming => General => Topic started by: bobonwhidbey on March 28, 2018, 11:46:53 pm

Title: [Solved] Tab stops in RichMemo
Post by: bobonwhidbey on March 28, 2018, 11:46:53 pm
RichMemo has several functions dealing with initializing, getting, and setting paragraph tabs. It appears to me there is a way for different tabs to handle different widths of columns. I have not seen any examples or documentation on this very nice feature. The default seems to be 5 characters per tab (or maybe it's based on pixel width). How can I set up my RichMemo so that the first column is a narrow 3 characters, and the next 5 columns are 15 characters?
Title: Re: Tab stops in RichMemo
Post by: rvk on March 29, 2018, 12:26:45 pm
There is TRichMemo.SetParaTabs() and TRichMemo.GetParaTabs() to set and get the tabs.

How can I set up my RichMemo so that the first column is a narrow 3 characters, and the next 5 columns are 15 characters?
You can't set it at "characters" because with a proportional font the characters are all different width. Do you want 15 i's or 15 M's?

You need to set the tabs to a distance in points from the left side of the control.

http://wiki.lazarus.freepascal.org/RichMemo#SetParaTabs

Example. It's not exact so you need to fiddle with the "character width". You also need to set the start and end of the paragraph for which these tabs are meant for.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   StopList: TTabStopList;
  4.   ChrW: Integer;
  5. begin
  6.   ChrW := Canvas.TextWidth('W');
  7.   InitTabStopList(StopList, [3 * ChrW, (3 + 15) * ChrW, (3 + 30) * ChrW, (3 + 45) * ChrW, (3 + 60) * ChrW, (3 + 75) * ChrW]);
  8.   // you can also set the StopList.Tabs[].Offset manually in a loop
  9.   RichMemo1.SetParaTabs(1, 9999, StopList);
  10. end;
  11.  
  12. procedure TForm1.Button2Click(Sender: TObject);
  13. var
  14.   StopList: TTabStopList;
  15.   I: integer;
  16. begin
  17.   if RichMemo1.GetParaTabs(1, StopList) then
  18.   begin
  19.     for I := 0 to StopList.Count - 1 do
  20.     begin
  21.       RichMemo1.Lines.Add(format('%f', [StopList.Tabs[I].Offset]));
  22.     end;
  23.   end;
  24. end;
Title: Re: Tab stops in RichMemo
Post by: bobonwhidbey on March 29, 2018, 05:20:24 pm
Very nice feature. Thank you rvk. It would be good to put this in one of the RichMemo examples.
Title: Re: [Solved] Tab stops in RichMemo
Post by: Nicole on March 27, 2023, 06:05:28 pm
... and useful for many years, thanks.
TinyPortal © 2005-2018