Bonjour à tous,
je suis en train de créer un composant qui utilise les TCollection. Je suis presqu' arrivé à quelque chose qui me convient mais cependant il me reste un message d'erreur qui apparaît quand je veux ajouter un TColletionItem à ma collection. voici mes classes un peu simplifiées:
et les implémentations des méthodes:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 TIndicator = class(TCollectionItem) private FSesTimeLine: TSesTimeLine; FDateTime: TDateTime; ... protected function GetDisplayName : string; override; public constructor Create(Collection: TCollection); override; destructor Destroy(); Override; procedure Assign(Source : TPersistent); override; ... published ... end; {CLASS TIndicators} TIndicators = class(TCollection) private FSesTimeLine: TSesTimeLine; function GetItem(Index: Integer): TIndicator; procedure SetItem(Index: Integer; Value: TIndicator); protected function GetOwner : TPersistent; override; procedure Update(Item : TCollectionItem); override; ... public constructor Create(ASesTimeLine: TSesTimeLine); function Add: TIndicator; function AddItem(Item: TIndicator; Index: Integer): TIndicator; function Insert(Index: Integer): TIndicator; ... end; {CLASS TSesTimeLine} TSesTimeLine = class(TCustomControl) private { Déclarations privées } FOwner: TComponent; FIndicators: TIndicators; ... procedure DrawBackground(Canvas: TCanvas); ... protected { Déclarations protégées } procedure Paint(); override; procedure Resize; override; ... public { Déclarations publiques } constructor Create(AOwner:TComponent); override; {CREATE} destructor Destroy(); Override; procedure Loaded; override; {Add Indicator} procedure AddIndicator(ADateTime: TDateTime; AID:Integer; AKind: Integer; ADisplayText: String); ... published { Déclarations publiées } ... {Indicators} property Indicators: TIndicators read FIndicators write SetIndicators; ... {custom events} property OnIndicatorClick: TIndicatorClick read FOnIndicatorClick write FOnIndicatorClick; end;
Donc si j'utilise la méthode 1 ou 2 de TSesTimeLine.AddIndicator, j'ai une violation d'accès à l'adresse 004BBCF1 dans le module Test.exe. Lecture de l'adresse 00000299. En mode debug, la violation intervient sur la ligne:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 constructor TIndicator.Create(Collection : TCollection); begin inherited Create(Collection); FSesTimeLine := TIndicators(Collection).FSesTimeLine; ... end; constructor TIndicators.Create(ASesTimeLine: TSesTimeLine); begin inherited Create(TIndicator); FSesTimeLine:=ASesTimeLine; end; function TIndicators.Add: TIndicator; begin RESULT:=TIndicator(inherited Add); {RESULT} end; function TIndicators.AddItem(Item: TIndicator; Index: Integer): TIndicator; begin if Item = nil then Result := Add {RESULT} else Result := Item; {RESULT} if Assigned(Result) then begin Result.Collection := Self; if Index < 0 then Index := Count - 1; Result.Index := Index; {RESULT} end; end; constructor TSesTimeLine.Create(AOwner: TComponent); begin inherited Create(AOwner); {avoid flicker} ControlStyle:=ControlStyle + [csOpaque]; {Init} FOwner:=AOwner; ... {Indicator} FIndicators:=TIndicators.Create(Self); end; destructor TSesTimeLine.Destroy; begin FIndicators.Free; inherited; end; procedure TSesTimeLine.AddIndicator(ADateTime: TDateTime; AID, AKind: Integer; ADisplayText: String); var Kind: TIndicatorKind; Indicator: TIndicator; begin {on récupère le type} Kind:=TIndicatorKind(AKind); {Methode 1} Indicator:=TIndicator.Create(FIndicators); Indicator.Width:=120; Indicator.Height:=20; Indicator.DateTime:=ADateTime; Indicator.ID:=AID; Indicator.Kind:=TIndicatorKind(AKind); Indicator.Text:=ADisplayText; FIndicators.AddItem(Indicator,-1); {Methode 2} // Indicator:=FIndicators.Add(); // Indicator.Width:=120; //Random(100); // Indicator.Height:=20; // Indicator.DateTime:=ADateTime; // Indicator.ID:=AID; // Indicator.Kind:=TIndicatorKind(AKind); // Indicator.Text:=ADisplayText; end;
du constructeur constructor TIndicator.Create(Collection : TCollection);
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 inherited Create(Collection);
Après avoir cherché pas mal sur le net, et regardé attentivement les tuto de création de composant sur Developez, je n'ai rien trouvé pour solutionner mon problème. Quelqu'un a t'il une piste ? merci d'avance.
Vincent
Partager