Modifier des objets créés dynamiquement

Résolu
EutcheX Messages postés 14 Date d'inscription mercredi 9 juin 2004 Statut Membre Dernière intervention 13 janvier 2007 - 13 janv. 2007 à 04:29
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 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

8 réponses

EutcheX Messages postés 14 Date d'inscription mercredi 9 juin 2004 Statut Membre Dernière intervention 13 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 !
3
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
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;





<hr size="2" width="100%" />
Croc (click me)
0
DRJEROME Messages postés 436 Date d'inscription jeudi 9 janvier 2003 Statut Membre Dernière intervention 5 février 2015
13 janv. 2007 à 12:00
Salut,
et
<hr />(findcomponent('TonStaticText') as TStaticText).color:=clred;   // ne marche pas ?

sinon essaie (si c'est dans une function) :

(Classes.findcomponent('TonStaticText') as TStaticText).color:=clred; 
<hr />
J'ai  peut-être mal compris la question (dans ce cas là je m'en excuse)

DrJerome
0
japee Messages postés 1727 Date d'inscription vendredi 27 décembre 2002 Statut Modérateur Dernière intervention 6 novembre 2021 8
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
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
japee Messages postés 1727 Date d'inscription vendredi 27 décembre 2002 Statut Modérateur Dernière intervention 6 novembre 2021 8
13 janv. 2007 à 12:30
Mais je viens de regarder de plus près la solution de f0xi, là c'est sûr tu as la totale...
0
EutcheX Messages postés 14 Date d'inscription mercredi 9 juin 2004 Statut Membre Dernière intervention 13 janvier 2007
13 janv. 2007 à 15:39
Merci à tous de vos réponses. mais aucune ne marche, sauf celle de f0xi :)

Nous n'héritons pas de la Terre, mais l'empruntons à nos Enfants !
0
cs_Delphiprog Messages postés 4297 Date d'inscription samedi 19 janvier 2002 Statut Membre Dernière intervention 9 janvier 2013 32
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.

May Delphi be with you !
<hr color="#008000" />http://www.afipa.net/
0
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
14 janv. 2007 à 19:59
alors a un moment il faut faire comme cela :

<hr size= "2" width="100%" />

Unit LblPanel;

INTERFACE

{ remove the dot for registering TLabelPanel class }
{.$DEFINE REG_TLabelPanel}

{$IFDEF REG_TLabelPanel}
  { remove the dot for include the palette icon resource for TLabelPanel }
  {.$DEFINE ICO_TLabelPanel}

  {$IFDEF ICO_TLabelPanel}
     {$R LblPanel.dcr}
  {$ENDIF}
{$ENDIF}

Uses Windows, SysUtils, Classes, Controls, StdCtrls, ExtCtrls;

Type
  { TLPLabel ------- }
  { TLPLabel is used in TCustomLabelPanel class, modify this for add properties, methods ect...}
  TLPLabel = class(TStaticText);

  { TCustomLabelPanel ------- derivate me! }
  TCustomLabelPanel = class(TPanel)
  private
    {-----}
    fLabels        : array[0..9] of TLPLabel;
    fLblSpc        : integer;
    fOnLabelChange : TNotifyEvent;
    {-----}
    procedure SetLabel(index : integer; Val : TLPLabel);
    function  GetLabel(index : integer) : TLPLabel;
    procedure SetLblSpc(val : integer);
    function  GetCount : integer;
  protected
    {-----}
    property  OnLabelChange : TNotifyEvent read fOnLabelChange write fOnLabelChange;
    procedure LabelChange(const index : integer); virtual;
    {-----}
    property  LabelsSpacing : integer read fLblSpc write SetLblSpc default 5;
    procedure AdjustLabelTop(const Spacing : integer); virtual;
    procedure AdjustPanelHeight(const Spacing : integer); virtual;
    {-----}
    property  Count : integer  read GetCount;
  public
    constructor Create(AOwner : TComponent); override;
    destructor  Destroy; override;
    {-----}
    property Items[index : integer] : TLPLabel read GetLabel write SetLabel;
  end;

  { TLabelPanel ------- use me! }
  TLabelPanel = class(TCustomLabelPanel)
  public
    property Count;
  published
    property LabelsSpacing;
  end;

{$IFDEF REG_TLabelPanel}
procedure Register;
{$ENDIF}

IMPLEMENTATION

{$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;

end.

<hr size="2" width="100%" />
Classes powaaaa!

<hr size ="2" width="100%" />Croc (click me)
0
Rejoignez-nous