File: example2.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (118 lines) | stat: -rw-r--r-- 2,745 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
{
 *****************************************************************************
  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************

  Author: Mattias Gaertner

  Abstract:
    Example for RTTI controls.
    Demonstrates, how to define a custom class with custom types and connecting
    it to RTTI controls.
}
unit Example2;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LCLProc, LResources, TypInfo, Forms, Controls, Graphics,
  Dialogs, RTTICtrls, StdCtrls;

type
  TMyEnum = (MyEnum1,MyEnum2,MyEnum3);
  TMyRange = 3..7;

  TMyClass = class(TPersistent)
  private
    FMyEnum: TMyEnum;
    FMyRange: TMyRange;
    FMyString: string;
    procedure SetMyEnum(const AValue: TMyEnum);
    procedure SetMyRange(const AValue: TMyRange);
    procedure SetMyString(const AValue: string);
  published
    property MyString: string read FMyString write SetMyString;
    property MyEnum: TMyEnum read FMyEnum write SetMyEnum;
    property MyRange: TMyRange read FMyRange write SetMyRange;
  end;

  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Memo1: TMemo;
    TIEdit1: TTIEdit;
    TIRadioGroup1: TTIRadioGroup;
    TITrackBar1: TTITrackBar;
    procedure Form1Create(Sender: TObject);
    procedure Form1Destroy(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    MyObject: TMyClass;
  end; 

var
  Form1: TForm1; 
  
procedure Log(const Msg: string);

implementation

{$R example2.lfm}

procedure Log(const Msg: string);
begin
  Form1.Memo1.Lines.Add(Msg);
end;

{ TForm1 }

procedure TForm1.Form1Create(Sender: TObject);
begin
  // create MyObject
  MyObject:=TMyClass.Create;
  // link properties
  TIEdit1.Link.SetObjectAndProperty(MyObject,'MyString');
  TIRadioGroup1.Link.SetObjectAndProperty(MyObject,'MyEnum');
  TITrackBar1.Link.SetObjectAndProperty(MyObject,'MyRange');
end;

procedure TForm1.Form1Destroy(Sender: TObject);
begin
  // unlink properties
  TIEdit1.Link.TIObject:=nil;
  TIRadioGroup1.Link.TIObject:=nil;
  TITrackBar1.Link.TIObject:=nil;
  // free MyObject
  MyObject.Free;
end;

{ TMyClass }

procedure TMyClass.SetMyEnum(const AValue: TMyEnum);
begin
  if AValue=MyEnum then exit;
  FMyEnum:=AValue;
  Log('TMyClass.SetMyEnum '+GetEnumProp(Self,'MyEnum'));
end;

procedure TMyClass.SetMyRange(const AValue: TMyRange);
begin
  if AValue=MyRange then exit;
  FMyRange:=AValue;
  Log('TMyClass.SetMyRange '+IntToStr(MyRange));
end;

procedure TMyClass.SetMyString(const AValue: string);
begin
  if AValue=MyString then exit;
  FMyString:=AValue;
  Log('TMyClass.SetMyString '+MyString);
end;

end.