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

Composants VCL Delphi Discussion :

Changement de couleur d'une ligne d'un stringgrid/D7 perso


Sujet :

Composants VCL Delphi

  1. #21
    Membre confirmé
    Avatar de OutOfRange
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    533
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 533
    Points : 474
    Points
    474
    Par défaut
    Excuse-moi d'insister mais ta condition
    if (stringgrid1.Selection.Left>=stringgrid1.FixedCols) and (stringgrid1.Selection.Right>(stringgrid1.ColCount-1))
    à mon avis est toujours fausse quelle que soit la sélection puisque
    (stringgrid1.Selection.Right>(stringgrid1.ColCount-1)
    n'est jamais vraie
    As-tu essayé de lancer ton appli avec un point d'arrêt sur la ligne
    pour t'assurer qu'elle est bien exécutée ?
    Choisir, c'est renoncer...

  2. #22
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    187
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 187
    Points : 69
    Points
    69
    Par défaut
    Toute mes conditions fonctionnent, je t'assure tout est vérifier.

    C'est juste le code dans ondrawcell qui n'est pas bon.

  3. #23
    Membre confirmé
    Avatar de OutOfRange
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    533
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 533
    Points : 474
    Points
    474
    Par défaut
    Essaie en remplaçant dans ondrawcell
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    for i:=1 to stringgrid1.Row-1 do
    if arow in [i] then
    begin
    par
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if arow=stringgrid1.Row
    Choisir, c'est renoncer...

  4. #24
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    187
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 187
    Points : 69
    Points
    69
    Par défaut
    Il y a du mieux, mais je change la couleur de la cellule de la colonne la plus à gauche et pas la ligne complète, ensuite en changeant la couleur d'une selection j'annule les changements de couleurs des ligne suivantes.

  5. #25
    Membre expert
    Avatar de LadyWasky
    Femme Profil pro
    Inscrit en
    Juin 2004
    Messages
    2 932
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 53
    Localisation : France, Hauts de Seine (Île de France)

    Informations forums :
    Inscription : Juin 2004
    Messages : 2 932
    Points : 3 565
    Points
    3 565
    Par défaut
    Voici ce que je propose (chez moi ça marche nickel ) :
    Dans une fiche, j'ai mis un bouton, un StringGrid, et c'est tout.
    Ici, tu peux non seulement sélectionner une ligne, tu peux aussi déselectionner

    Dans cette version (il y en a une 2nde plus loin, moins limitée), comme ça marche avec un ensemble, qui représente les lignes effectivement sélectionnées. Ca ne fonctionne qu'un StringGrid de 256 lignes maximum puisque qu'un ensemble ne peut se faire que sur des ordinaux de 256 éléments maximum
    Enfin, on dira que c'est la version "didactique" qui permet de comprendre la suivante

    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
    unit Unit1;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Grids;
     
    type
      TForm1 = class(TForm)
        StringGrid1: TStringGrid;
        Button1: TButton;
        procedure FormCreate(Sender: TObject);
        procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
          var CanSelect: Boolean);
        procedure Button1Click(Sender: TObject);
        procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
      private
        { Déclarations privées }
      public
        { Déclarations publiques }
        RowsSelected:set of byte;
        LastRowClicked:integer;
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
     
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      RowsSelected:=[];
      LastRowClicked:=-1;
    end;
     
    procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
      ARow: Integer; var CanSelect: Boolean);
    begin
      LastRowClicked:=ARow;
    end;
     
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      if LastRowClicked=-1 then Exit; //on a rien sélectionné encore !
      if LastRowClicked>255 then exit; //sinon ça plante et c'est normal
      if LastRowClicked in RowsSelected
      then RowsSelected:=RowsSelected-[LastRowClicked]
      else RowsSelected:=RowsSelected+[LastRowClicked];
      StringGrid1.Invalidate;
    end;
     
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      if ARow in RowsSelected then
      begin
        StringGrid1.Canvas.Brush.Color:=clRed;
        stringgrid1.canvas.fillrect(rect);
        stringgrid1.canvas.TextOut(Rect.Left,Rect.Top,stringgrid1.Cells[ACol,ARow]);
      end;
    end;
     
    end.

    Dans cette deuxième version, j'ai remplacé l'ensemble par TIntegerList (qu'il faut écrire soit même puisque Delphi n'en fourni pas ), d'ailleurs je l'ai écris
    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
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    unit Unit1;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Grids;
     
    type
      TIntegerList = class( TList )
      private
        procedure SetInteger( Index: Integer; Value: Integer );
        function  GetInteger( Index: Integer ): Integer;
      public
        procedure Add( AInteger: Integer );
        function  First: Integer;
        function  Last: Integer;
        function  IndexOf( AInteger: Integer ): Integer;
        procedure Insert( Index: Integer; AInteger: Integer );
        function  Remove( AInteger: Integer ): Integer;
     
        property Integers[Index: Integer]: Integer read GetInteger write
    SetInteger; default;
      end;
     
      TForm1 = class(TForm)
        StringGrid1: TStringGrid;
        Button1: TButton;
        procedure FormCreate(Sender: TObject);
        procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
          var CanSelect: Boolean);
        procedure Button1Click(Sender: TObject);
        procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
        procedure FormDestroy(Sender: TObject);
      private
        { Déclarations privées }
      public
        { Déclarations publiques }
        RowsSelected:TIntegerList;
        LastRowClicked:integer;
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
     
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      RowsSelected:=TIntegerList.Create;
      LastRowClicked:=-1;
    end;
     
    procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
      ARow: Integer; var CanSelect: Boolean);
    begin
      LastRowClicked:=ARow;
    end;
     
    procedure TForm1.Button1Click(Sender: TObject);
    var index:integer;
    begin
      if LastRowClicked=-1 then Exit;
      index:=RowsSelected.IndexOf(LastRowClicked);
      if index>-1
      then RowsSelected.Delete(index)
      else RowsSelected.Add(LastRowClicked);
      StringGrid1.Invalidate;
    end;
     
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      if RowsSelected.IndexOf(ARow)>-1 then
      begin
        StringGrid1.Canvas.Brush.Color:=clRed;
        stringgrid1.canvas.fillrect(rect);
        stringgrid1.canvas.TextOut(Rect.Left,Rect.Top,stringgrid1.Cells[ACol,ARow]);
      end;
    end;
     
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
      RowsSelected.Free;
    end;
     
    //Implementation de TintegerList
    procedure TIntegerList.SetInteger( Index: Integer; Value: Integer );
    begin
      inherited Items[Index] := Pointer( Value );
    end;
     
    function TIntegerList.GetInteger( Index: Integer ): Integer;
    begin
      Result := Integer( inherited Items[Index] );
    end;
     
    procedure TIntegerList.Add( AInteger: Integer );
    begin
      inherited Add( Pointer(AInteger) );
    end;
     
    function TIntegerList.First: Integer;
    begin
      //if (Count > 0) then
        Result := Integers[0];
    end;
     
    function TIntegerList.Last: Integer;
    begin
      //if (Count > 0) then
        Result := Integers[Count-1];
    end;
     
    function TIntegerList.IndexOf( AInteger: Integer ): Integer;
    begin
      Result := inherited IndexOf( Pointer(AInteger) );
    end;
     
    procedure TIntegerList.Insert( Index: Integer; AInteger: Integer );
    begin
      inherited Insert( Index, Pointer(AInteger) );
    end;
     
    function TIntegerList.Remove( AInteger: Integer ): Integer;
    begin
      Result := inherited Remove( Pointer(AInteger) );
    end;
     
     
     
    end.
    En espérant que celà rejoigne ton besoin, je te souhaites bon dev'
    Bidouilleuse Delphi

  6. #26
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    187
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 187
    Points : 69
    Points
    69
    Par défaut
    Bonjour,

    Je te remercie, effectivement ton code avec quelques modif me permet d'obtenir ce que je veux et je t'en remercie, mais je suis certain qu'il existe un code beaucoup plus simple.

    Je me trouve sur une ligne de mon stringgrid et en clickant sur un bouton je change la couleur de ma ligne et inversement c'est pas plus mal.


    ?????????

    Jer

  7. #27
    Membre expert
    Avatar de LadyWasky
    Femme Profil pro
    Inscrit en
    Juin 2004
    Messages
    2 932
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 53
    Localisation : France, Hauts de Seine (Île de France)

    Informations forums :
    Inscription : Juin 2004
    Messages : 2 932
    Points : 3 565
    Points
    3 565
    Par défaut
    Citation Envoyé par jer64
    Je te remercie, effectivement ton code avec quelques modif me permet d'obtenir ce que je veux et je t'en remercie, mais je suis certain qu'il existe un code beaucoup plus simple.
    Ca m'étonnerais fortement de pouvoir faire plus simple avec exactement le même résultat.
    Bref, en trois ligne de codes, ton truc, ce n'est pas faisable !!!

    Je ne vois pas du tout ce qu'il y a de compliqué, à part le fait qu'il n'existe pas de TIntegerList tout fait en Delphi et qu'on doit en faire un (que tu peux mettre dans une unité à part pour clarifier le code...)
    Bidouilleuse Delphi

  8. #28
    Membre confirmé

    Inscrit en
    Novembre 2002
    Messages
    744
    Détails du profil
    Informations forums :
    Inscription : Novembre 2002
    Messages : 744
    Points : 500
    Points
    500
    Par défaut
    Resalut ,

    peut etre cela peut il d'aider , quand tu clique sur le bouton tu colores la ligne selectionnée
    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
     
    ....
    Var Ligne:longint=-1;
     
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
        with StringGrid1.Canvas do
        begin
            brush.color:=clwhite;
            if Arow=ligne then brush.color:=clred;
            fillrect(rect);
            brush.Style:=BsClear;
            textout(rect.left+2,rect.top+2,StringGrid1.cells[Acol,Arow])
        end;
    end;
     
    procedure TForm1.BitBtn1Click(Sender: TObject);
    begin
        Ligne:=StringGrid1.Selection.Top;
        StringGrid1.repaint;
    end;
    Bye et bon code...

    Ce n'est pas tant l'aide de nos amis qui nous aide , mais notre confiance dans cette aide .

  9. #29
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    187
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 187
    Points : 69
    Points
    69
    Par défaut
    Contrairement au code de Waskol, le code de petitcoucou31 ne maintient pas la couleur de la ligne lorsque l'on relance l'intruction sur une nouvelle ligne.

  10. #30
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    187
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 187
    Points : 69
    Points
    69
    Par défaut
    Bon, on ne va pas être plus royaliste que le roi, j'ai adapté le code de Waskol, cela marche parfaitement.

    Je vous remercie pour votre aide et surtout Waskol.

    A+

    Jer

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

Discussions similaires

  1. Réponses: 1
    Dernier message: 02/02/2014, 01h34
  2. Changement de couleur d'une ligne dans un Memo
    Par andreditdd dans le forum Composants VCL
    Réponses: 8
    Dernier message: 19/08/2008, 13h47
  3. [DataGridView] Bug dans le changement de couleur d'une ligne
    Par AsPrO dans le forum Windows Forms
    Réponses: 8
    Dernier message: 22/07/2008, 23h38
  4. Réponses: 9
    Dernier message: 12/08/2002, 07h38
  5. String Grid et choix d'une couleur pour une ligne
    Par Gigottine dans le forum C++Builder
    Réponses: 12
    Dernier message: 17/05/2002, 15h23

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