home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue154 / delphi / tabstops / TS.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1999-04-27  |  2.1 KB  |  80 lines

  1. unit ts;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Memo1: TMemo;
  12.     Memo2: TMemo;
  13.     Button1: TButton;
  14.     NoTabStops: TButton;
  15.     procedure NoTabStopsClick(Sender: TObject);
  16.     procedure Memo1KeyDown(Sender: TObject; var Key: Word;
  17.       Shift: TShiftState);
  18.     procedure Memo1KeyPress(Sender: TObject; var Key: Char);
  19.     procedure Memo1KeyUp(Sender: TObject; var Key: Word;
  20.       Shift: TShiftState);
  21.   private
  22.     { Private declarations }
  23.   public
  24.     { Public declarations }
  25.     procedure GetCursorPos( var LinePos, ColPos : LongInt );
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.DFM}
  34.  
  35. procedure TForm1.GetCursorPos( var LinePos, ColPos : LongInt );
  36. begin
  37.    LinePos := SendMessage(Memo1.Handle, EM_LINEFROMCHAR, Memo1.SelStart,0);
  38.    ColPos := (SendMessage(Memo1.Handle, EM_LINEINDEX, LinePos, 0));
  39.    ColPos := Memo1.SelStart - ColPos;
  40. end;
  41.  
  42. procedure TForm1.NoTabStopsClick(Sender: TObject);
  43. var
  44.    i : integer;
  45. begin
  46.    for i := 0 to ComponentCount - 1 do
  47.        TWinControl(Components[i]).TabStop := false;
  48. end;
  49.  
  50. procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
  51.   Shift: TShiftState);
  52. begin
  53.    Memo2.Lines.Add( 'Memo1KeyDown: Key = ' + IntToStr(Key) );
  54. end;
  55.  
  56. procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
  57. begin
  58.    Memo2.Lines.Add( 'Memo1KeyPress: Key = ' + Key );
  59. end;
  60.  
  61. procedure TForm1.Memo1KeyUp(Sender: TObject; var Key: Word;
  62.   Shift: TShiftState);
  63. var
  64.    LineNum, ColNum, StartPos : LongInt;
  65.    aLine : string;
  66. begin
  67.    Memo2.Lines.Add( 'Memo1KeyUp: Key = ' + IntToStr(Key));
  68.    if Key = 9 then
  69.       begin
  70.         GetCursorPos( LineNum, ColNum );
  71.         StartPos := Memo1.selStart;
  72.         aLine := Memo1.Lines[LineNum];      { get current line from Memo  }
  73.         insert(^I, aLine, ColNum+1 );            { insert tab character   }
  74.         Memo1.Lines[LineNum] := aLine;      { put altered line in Memo    }
  75.         Memo1.selStart := StartPos + Length(^I);{ put cursor after tab    }
  76.       end;
  77. end;
  78.  
  79. end.
  80.