IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 Delphi Discussion :

Problème en créant dynamiquement un Panel


Sujet :

Delphi

  1. #1
    Membre actif Avatar de Basile le disciple
    Homme Profil pro
    étudiant Centrale Supélec
    Inscrit en
    Avril 2013
    Messages
    147
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 25
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : étudiant Centrale Supélec

    Informations forums :
    Inscription : Avril 2013
    Messages : 147
    Points : 279
    Points
    279
    Par défaut Problème en créant dynamiquement un Panel
    Bonjour tout le monde

    Voici mon problème: j'ai créé dynamiquement un Panel dans le OnCreate de la Form et j'ai donné ses propriétés Width et Left dans le OnResize(de la Form).

    Mais dès que je compile le projet, Delphi m'adresse un message d'erreur et je ne sais pas pourquoi!!

    Voici mon code :

    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
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
     
    unit Unit1;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, StdCtrls;
     
    type
      TForm1 = class(TForm)
        GroupBox1: TGroupBox;
        procedure FormResize(Sender: TObject);
        procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
        procedure FormCreate(Sender: TObject);
      private
        { Déclarations privées }
        Panels : array[0..6] of TPanel;
        procedure changePolicePanel;
        procedure PanelsMouseMove(Sender: TObject; Shift: TShiftState;X, Y: Integer);
      public
        { Déclarations publiques }
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
     
    procedure TForm1.FormResize(Sender: TObject);
    var i : integer;
    begin
      For i:=0 to 6 do
      begin
        Panels[i].Left:=i*ClientWidth div 6;
        Panels[i].Width:=ClientWidth div 6;
      end;
      With GroupBox1 do
      begin
        Top:=50;
        Height:=self.ClientHeight-Top; Width:=self.ClientWidth div 5;
      end;
    end;
     
    procedure TForm1.changePolicePanel;
    var i : integer;
    begin
     For i:=0 to 6 do
     begin
       Panels[i].Font.Size:=round(Panels[i].Width*Panels[i].Height*10/(107*50));
       Panels[i].Font.Style:=[];
     end;
    end;
     
    procedure TForm1.PanelsMouseMove(Sender: TObject; Shift: TShiftState;X, Y: Integer);
    begin
      ChangePolicePanel;
      With TPanel(sender) do
      begin
        Font.Size:=round(Width*Height*10/(107*50)+2);
        Font.Style:=[fsUnderLine];
      end;
    end;
     
     
    procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin
      ChangePolicePanel;
    end;
     
    procedure TForm1.FormCreate(Sender: TObject);
    var i : integer;
    begin
      Width:=Screen.Width;
      Height:=Screen.Height;
      For i:=0 to 6 do
      begin
        Panels[i]:=TPanel.Create(self);
        With Panels[i] do
        begin
          Parent:=self;
          Top:=0; Height:=50;
          Color:=clLime; Cursor:=CrHandPoint;
          Font.Color:=clRed;  Font.Name:='Arial';
          Case i of
            0 : Caption:='Afrique';
            1 : Caption:='Asie';
            2 : Caption:='Amérique';
            3 : Caption:='Europe';
            4 : Caption:='Océanie';
            5 : Caption:='France';
            6 : Caption:='Plus';
          end;
          OnMouseMove:=PanelsMouseMove;
        end;
      end;
    end;
     
    end.
    Est-ce que le OnResize passe avant le OnCreate? Dans ce cas là, ça expliquerait tout.

  2. #2
    Membre averti Avatar de archonte
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    341
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 341
    Points : 392
    Points
    392
    Par défaut
    Salut Basile Le Disciple !

    En fait, tu fais appel à OnResize dès le début de ton FormCreate par l'instruction Sinon, effectivement, pour info l'ordre d'appel des procédures en Delphi est :
    OnCreate -> OnShow -> OnActivate -> OnPaint -> OnResize -> OnPaint ...
    ... OnCloseQuery -> OnClose -> OnDeactivate -> OnHide -> OnDestroy

    réf
    De plus, je note que tu as 7 panels et non 6 , si tu veux tous les visualiser dans ta procédure FormResize ...
    "Je n'ai jamais rencontré d'homme si ignorant qu'il n'eut quelque chose à m'apprendre."
    Galilée

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    707
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 707
    Points : 777
    Points
    777
    Par défaut
    Bonjour,

    Normalement OnResize devrait bien être exécuté après OnCreate:
    http://www.askingbox.com/info/delphi...e-to-ondestroy

    Mais tu peux contourner en mettant un "flag":

    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
    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
     
    unit Unit1;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, StdCtrls;
     
    type
      TForm1 = class(TForm)
        GroupBox1: TGroupBox;
        procedure FormResize(Sender: TObject);
        procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
        procedure FormCreate(Sender: TObject);
      private
        { Déclarations privées }
        bFormCreated: Boolean;
        Panels : array[0..6] of TPanel;
        procedure changePolicePanel;
        procedure PanelsMouseMove(Sender: TObject; Shift: TShiftState;X, Y: Integer);
      public
        { Déclarations publiques }
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
     
    procedure TForm1.FormResize(Sender: TObject);
    var i : integer;
    begin
    	if bFormCreated then begin
    	  For i:=0 to 6 do
        begin
          Panels[i].Left:=i*ClientWidth div 6;
          Panels[i].Width:=ClientWidth div 6;
        end;
      end;
      With GroupBox1 do
      begin
        Top:=50;
        Height:=self.ClientHeight-Top; Width:=self.ClientWidth div 5;
      end;
    end;
     
    procedure TForm1.changePolicePanel;
    var i : integer;
    begin
     For i:=0 to 6 do
     begin
       Panels[i].Font.Size:=round(Panels[i].Width*Panels[i].Height*10/(107*50));
       Panels[i].Font.Style:=[];
     end;
    end;
     
    procedure TForm1.PanelsMouseMove(Sender: TObject; Shift: TShiftState;X, Y: Integer);
    begin
      ChangePolicePanel;
      With TPanel(sender) do
      begin
        Font.Size:=round(Width*Height*10/(107*50)+2);
        Font.Style:=[fsUnderLine];
      end;
    end;
     
     
    procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin
      ChangePolicePanel;
    end;
     
    procedure TForm1.FormCreate(Sender: TObject);
    var i : integer;
    begin
    	bFormCreated := FALSE;
     
      Width := Screen.Width;
      Height := Screen.Height;
      For i:=0 to 6 do
      begin
        Panels[i]:=TPanel.Create(self);
        With Panels[i] do
        begin
          Parent:=self;
          Top:=0; Height:=50;
          Color:=clLime; Cursor:=CrHandPoint;
          Font.Color:=clRed;  Font.Name:='Arial';
          Case i of
            0 : Caption:='Afrique';
            1 : Caption:='Asie';
            2 : Caption:='Amérique';
            3 : Caption:='Europe';
            4 : Caption:='Océanie';
            5 : Caption:='France';
            6 : Caption:='Plus';
          end;
          OnMouseMove:=PanelsMouseMove;
        end;
      end;
     
      bFormCreated := TRUE;
    end;
     
    end.
    PS: bien vu archonte !

  4. #4
    Membre averti Avatar de archonte
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    341
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 341
    Points : 392
    Points
    392
    Par défaut
    En fait, vu que la dimension de la fenêtre n'est pas utilisée lors de la création des Panels[i], j'aurais tout simplement déplacé les 2 instructions en cause à la fin du FormCreate ...

    Citation Envoyé par Basile le disciple Voir le message
    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
    procedure TForm1.FormCreate(Sender: TObject);
    var i : integer;
    begin
    //  Width:=Screen.Width;
    //  Height:=Screen.Height;
      For i:=0 to 6 do
      begin
        Panels[i]:=TPanel.Create(self);
        With Panels[i] do
        begin
           (...)
        end;
          OnMouseMove:=PanelsMouseMove;
        end;
      end;
    // ici -->
      Width:=Screen.Width;
      Height:=Screen.Height;
     
    end;
    On peut même rajouter un ChangePolicePanel à la fin du FormResize ...
    "Je n'ai jamais rencontré d'homme si ignorant qu'il n'eut quelque chose à m'apprendre."
    Galilée

  5. #5
    Membre actif Avatar de Basile le disciple
    Homme Profil pro
    étudiant Centrale Supélec
    Inscrit en
    Avril 2013
    Messages
    147
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 25
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : étudiant Centrale Supélec

    Informations forums :
    Inscription : Avril 2013
    Messages : 147
    Points : 279
    Points
    279
    Par défaut
    Merci beaucoup pour vos réponses, c'est impeccable!!

    Merci aussi pour l'ordre d'appel des procédures, ça va m'être super utile!!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [HTML+CSS] Problème de menu "dynamique"
    Par Invité dans le forum Mise en page CSS
    Réponses: 2
    Dernier message: 06/04/2005, 12h48
  2. Question sur les problèmes d'allocation dynamique
    Par slylafone dans le forum C++
    Réponses: 23
    Dernier message: 25/10/2004, 14h18
  3. [JSP]Problème liste deroulante dynamique
    Par besco dans le forum Servlets/JSP
    Réponses: 1
    Dernier message: 09/09/2004, 17h58
  4. problèmes de textes dynamique dynamiquement générés
    Par stephane eyskens dans le forum Flash
    Réponses: 18
    Dernier message: 05/09/2003, 13h13
  5. [Rave Report] problème de création dynamique
    Par Nivux dans le forum Rave
    Réponses: 2
    Dernier message: 24/05/2003, 00h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo