EutcheX
Messages postés14Date d'inscriptionmercredi 9 juin 2004StatutMembreDernière intervention13 janvier 2007
-
13 janv. 2007 à 04:29
f0xi
Messages postés4205Date d'inscriptionsamedi 16 octobre 2004StatutModérateurDernière intervention12 mars 2022
-
14 janv. 2007 à 19:59
Bonjour à tous,
j'ai un petit soucis, ca fait quelques heures que je suis deçu et je commence à désespérer ... si quelqu'un pouvait m'aider ce serait super :D
Voilà, j'ai créé dynamiquement des objets (TStaticText), et je voudrais modifier la couleur de tous à l'aide d'un for. Le problème c' que Component[x] ne gère pas TStaticText : Color n'est pas déclaré...
J'ai essayé avec TStaticText(x).Color, le programme démarre mais affiche une erreur lors de l'execution de la procedure.
Merci d'avance à tous ;)
[font=Tahoma]Nous n'héritons pas de la Terre, mais l'empruntons à nos Enfants !/font=Tahoma
EutcheX
Messages postés14Date d'inscriptionmercredi 9 juin 2004StatutMembreDernière intervention13 janvier 2007 13 janv. 2007 à 23:22
C'est exacte Delphiprog pour ce qui est du code de f0xi, c' pour ca que j'ai pas accepté sa réponse ... j'ai juste testé son code et il a fonctionné. Pour mon problème, sûrement que la question était mal posée... à savoir que j'ai créé ces TStaticText avec un 'constructor' et non dans un tableau (j'sais pas si je m'explique encore bien?).
Enfin bref, j'ai trouvé une autre alternative à mon problème pour mon projet, je n'ai plus besoin d'utiliser les propriétés de mes TStaticText, du moins pour l'instant ...
Encore merci à tous de vos réponses ;)
Nous n'héritons pas de la Terre, mais l'empruntons à nos Enfants !
f0xi
Messages postés4205Date d'inscriptionsamedi 16 octobre 2004StatutModérateurDernière intervention12 mars 202235 13 janv. 2007 à 05:14
type
pStatics = ^TStatics;
TStatics = array of TStaticText;
procedure CreateStatics(const pStText : pStatics; Owner : TComponent; const Count : integer);
var N : integer;
begin
if Count = 0 then exit;
SetLength(pStText^, Count);
for N := 0 to Count-1 do
pStText^[N] := TStaticText.Create(Owner);
end;
procedure FreeStatics(const pStText : pStatics);
var N : integer;
begin
if Length(pStText^) = 0 then exit;
for N := 0 to High(pStText^) do
pStText^[N].Free;
SetLength(pStText^, 0);
end;
procedure SetStaticsTpl_All(const pStText : pStatics; StaticTpl : TStaticText);
var N : integer;
begin
if (Length(pStText^) = 0) or (not Assigned(StaticTpl)) then exit;
for N := 0 to High(pStText^) do
pStText^[N].Assign(StaticTpl);
end;
procedure SetStaticsTpl_Sel(const pStText : pStatics; StaticTpl : TStaticText; const Indexs : array of integer);
var N : integer;
begin
if (Length(pStText^) = 0) or
(not Assigned(StaticTpl)) or
(Length(Indexs) = 0) then exit;
for N := 0 to High(Indexs) do
pStText^[Indexs[N]].Assign(StaticTpl);
end;
procedure SetStaticsTpl_Ran(const pStText : pStatics; StaticTpl : TStaticText; const IndexStart,IndexStop : integer);
var N : integer;
begin
if (Length(pStText^) = 0) or
(not Assigned(StaticTpl)) or
(IndexStart < Low(pStText^)) or
(indexStop > high(pStText^)) then exit;
for N := IndexStart to IndexStop do
pStText^[N].Assign(StaticTpl);
end;
var
Statics : TStatics;
procedure TForm1.FormCreate(Sender: TObject);
begin
CreateStatics(@Statics, nil, 10);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeStatics(@Statics);
end;
japee
Messages postés1727Date d'inscriptionvendredi 27 décembre 2002StatutModérateurDernière intervention 6 novembre 20218 13 janv. 2007 à 12:27
Pourquoi ne pas faire, tout simplement :
procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
begin
for i := 0 to ComponentCount - 1 do
if Components[i] is TStaticText then
TStaticText(Components[i]).Color := clBlue;
//TStaticText(Components[i]).Font.Color := clRed; {couleur font}
end;
Bonne prog'
japee
Vous n’avez pas trouvé la réponse que vous recherchez ?
cs_Delphiprog
Messages postés4297Date d'inscriptionsamedi 19 janvier 2002StatutMembreDernière intervention 9 janvier 201332 13 janv. 2007 à 23:17
Ou les réponses ne sont pas celles que tu attendais ou la question était mal posée.
Les réponses de DRJerome et de Japee me semblent tout à fait adaptées et il s'agit là d'un code tellement basique que chacun est censé le connaitre sur le bout des doigts. En tous cas, c'est celles que j'aurais faites.
Si la solution de Foxi fonctionne, tant mieux. Mais ce n'est pas la plus simple à mettre en oeuvre et encore moins la plus souple sauf si tu connais d'avance le nombre de TStaticText à créer àlors de la conception. Ce qui remet forcément en cause l'intérêt de la création dynamique de composants...
Pensez à cliquer sur Réponse acceptée lorsque la réponse vous convient.
Type
{ TLPLabel ------- }
{ TLPLabel is used in TCustomLabelPanel class, modify this for add properties, methods ect...}
TLPLabel = class(TStaticText);
{$IFDEF REG_TLabelPanel}
procedure Register;
begin
RegisterComponents('TLabelPanel',[TLabelPanel]);
end;
{$ENDIF}
{ TCustomLabelPanel ------- }
constructor TCustomLabelPanel.Create(AOwner : TComponent);
var N : integer;
begin
inherited Create(AOwner);
{ Self Properties }
Caption := '';
Width := 250;
{ Self custom properties }
fLblSpc := 5;
{ StaticText create }
for N := 0 to high(fLabels) do begin
fLabels[N] := TLPLabel.Create(Self);
fLabels[N].Parent := Self;
{ StaticText properties }
with fLabels[N] do begin
Left := 5;
Width := 240;
Caption := 'Label '+IntToStr(N+1);
AutoSize := false;
Anchors := [akLeft, akTop, akRight];
end;
end;
{ Adjust size of Panel and vertical alignment of Labels }
AdjustPanelHeight(fLblSpc);
AdjustLabelTop(fLblSpc);
end;
destructor TCustomLabelPanel.Destroy;
var N : integer;
begin
for N := High(fLabels) downto 0 do
fLabels[N].Free;
inherited Destroy;
end;
procedure TCustomLabelPanel.SetLabel(index : integer; Val : TLPLabel);
begin
{ Assign new object }
fLabels[index].Assign(val);
{ Call change proc }
LabelChange(index);
end;
function TCustomLabelPanel.GetLabel(index : integer) : TLPLabel;
begin
{ return label at index }
result := fLabels[index];
end;
procedure TCustomLabelPanel.SetLblSpc(val : integer);
begin
{ modify the Labels vertical space }
if Val <> fLblSpc then begin
fLblSpc := Val;
AdjustPanelHeight(fLblSpc);
AdjustLabelTop(fLblSpc);
end;
end;
function TCustomLabelPanel.GetCount : integer;
begin
{ return length of fLabels array }
result := Length(fLabels);
end;
procedure TCustomLabelPanel.LabelChange(const index : integer);
begin
{ insert change methods here }
{ call change event }
if Assigned(fOnLabelChange) then
fOnLabelChange(Self);
end;
Procedure TCustomLabelPanel.AdjustLabelTop(const Spacing : integer);
var N,DV : integer;
begin
DV := (Height-10) div Length(fLabels);
for N := 0 to High(fLabels) do
fLabels[N].Top := 5+(DV*N);
end;
procedure TCustomLabelPanel.AdjustPanelHeight(const Spacing : integer);
var N,TH : integer;
begin
TH := 10;
for N := 0 to High(fLabels) do
TH := TH + fLabels[N].Height + Spacing;
Height := TH;
end;