home *** CD-ROM | disk | FTP | other *** search
/ Delphi Programming Unleashed / Delphi_Programming_Unleashed_SAMS_Publishing_1995.iso / misc / objects / edits / main.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-03-21  |  2.4 KB  |  113 lines

  1. unit Main;
  2.  
  3. { Copyright (c) 1995 by Charlie Calvert }
  4. { Project Name: Edits }
  5.  
  6. { This is just an exercise in dynamically creating
  7.   fonts and edit controls.
  8.  
  9.   Shows how to work with selections of text
  10.  
  11.   When using the program, create the edit control,
  12.   select some text, and press show selection. }
  13.  
  14. interface
  15.  
  16. uses
  17.   WinTypes, WinProcs, StdCtrls,
  18.   Classes, Graphics, Dialogs,
  19.   Controls, Forms, ExtCtrls;
  20.  
  21. type
  22.   TForm1 = class(TForm)
  23.     CreateEd: TButton;
  24.     Edit1: TEdit;
  25.     Edit2: TEdit;
  26.     Label1: TLabel;
  27.     Label2: TLabel;
  28.     ShowSel: TButton;
  29.     Edit3: TEdit;
  30.     Label3: TLabel;
  31.     Panel1: TPanel;
  32.     BoldBox: TCheckBox;
  33.     procedure CreateEdClick(Sender: TObject);
  34.     procedure ShowSelClick(Sender: TObject);
  35.     procedure BoldBoxClick(Sender: TObject);
  36.     procedure MyEditKeyPress(Sender: TObject; var Key: Char);
  37.   private
  38.     MyEdit: TEdit;
  39.     MyFont: TFont;
  40.   end;
  41.  
  42. var
  43.   Form1: TForm1;
  44.  
  45. implementation
  46.  
  47. {$R *.DFM}
  48.  
  49. function Int2Str(i: Integer): String;
  50. var
  51.   S: String;
  52. begin
  53.   Str(i, S);
  54.   result := S;
  55. end;
  56.  
  57. procedure TForm1.CreateEdClick(Sender: TObject);
  58. begin
  59.   MyFont := TFont.Create;
  60.   MyFont.Name := 'New Times Roman';
  61.   MyFont.Size := 25;
  62.   MyFont.Color := clGreen;
  63.  
  64.   MyEdit := TEdit.Create(Form1);
  65.   MyEdit.Parent := Form1;
  66.   MyEdit.SetFocus;
  67.   MyEdit.SetBounds(10, 10, 190, 45);
  68.   MyEdit.Color := clBlue;
  69.   MyEdit.Ctl3d := True;
  70.   MyEdit.Text := 'Samantha';
  71.   MyEdit.ReadOnly := True;
  72.   MyEdit.Cursor := CrCross;
  73.   MyEdit.OnKeyPress := MyEditKeyPress;
  74.   MyEdit.SelStart := 2;  { Tab to it to see it}
  75.   MyEdit.SelLength := 3;
  76.   MyEdit.Font := MyFont;
  77.  
  78.   BoldBox.Enabled := True;
  79.   ShowSel.Enabled := True;
  80.   CreateEd.Enabled := False;
  81. end;
  82.  
  83. procedure TForm1.ShowSelClick(Sender: TObject);
  84. var
  85.   Start, Len: Integer;
  86. begin
  87.   if MyEdit = nil then Exit;
  88.   Start := MyEdit.SelStart;
  89.   Len := MyEdit.SelLength;
  90.   Edit1.Text := Int2Str(Start);
  91.   Edit2.Text := Int2Str(Len);
  92.   Edit3.Text := MyEdit.SelText;
  93. end;
  94.  
  95. procedure TForm1.BoldBoxClick(Sender: TObject);
  96. begin
  97.   if BoldBox.Checked then
  98.     MyEdit.Font.Style := [fsBold, fsItalic]
  99.   else
  100.     MyEdit.Font.Style := [];
  101. end;
  102.  
  103. { Check to see if user pressed enter in control }
  104. procedure TForm1.MyEditKeyPress(Sender: TObject; var Key: Char);
  105. begin
  106.   if Key = #13 then begin
  107.     Key := #0;
  108.     MessageDlg(Edit1.Text, mtInformation, [mbOk], 0);
  109.   end;
  110. end;
  111.  
  112. end.
  113.