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 :

Gestion des clics sur plusieurs icônes dans une colonne DBGrid sous Delphi 7


Sujet :

Delphi

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre très actif
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2018
    Messages
    482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2018
    Messages : 482
    Par défaut Gestion des clics sur plusieurs icônes dans une colonne DBGrid sous Delphi 7
    Bonjour,

    Cette demande est liée à la discussion précédente :
    https://www.developpez.net/forums/d2...us-delphi-7-a/

    Je tiens à remercier infiniment XeGregory pour sa solution proposée qui fonctionne parfaitement. 🙏

    J’aimerais maintenant savoir comment gérer les clics sur chaque icône affichée dans la colonne (trois icônes : plus, moins, supprimer).

    Merci d’avance pour votre aide !
    Images attachées Images attachées  

  2. #2
    Membre expérimenté
    Avatar de XeGregory
    Homme Profil pro
    Passionné par la programmation
    Inscrit en
    Janvier 2017
    Messages
    506
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Passionné par la programmation
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Janvier 2017
    Messages : 506
    Par défaut
    Bonjour LandGreen

    J’aimerais maintenant savoir comment gérer les clics sur chaque icône affichée dans la colonne (trois icônes : plus, moins, supprimer).
    De cette façon, c'est tout simplement :

    Type TDBGridLandGreen class(TDBGrid)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    type
      TDBGridLandGreen = class(TDBGrid)
      end;
     
    TForm1 = class(TForm)
    // ...
    DBGrid1 OnDrawColumnCell
    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
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer;
      Column: TColumn; State: TGridDrawState);
    var
      IconLeft: Integer;
      IconTop: Integer;
      IconSpacing: Integer;
      IconWidth: Integer;
    begin
      if Column.Title.Caption = 'Actions' then
      begin
        IconSpacing := 4;
        IconWidth := ImageList1.Width;
        IconTop := Rect.Top + (Rect.Bottom - Rect.Top - ImageList1.Height) div 2;
        IconLeft := Rect.Left + IconSpacing;
     
        // Dessiner l'icône de décrémentation (index 0 de l'image list)
        ImageList1.Draw(DBGrid1.Canvas, IconLeft, IconTop, 0);
        Inc(IconLeft, IconWidth + IconSpacing);
     
        // Dessiner l'icône d'incrémentation (index 1 de l'image list)
        ImageList1.Draw(DBGrid1.Canvas, IconLeft, IconTop, 1);
      end
      else
        // Pour toutes les autres colonnes, on laisse le dessin par défaut
        DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
    end;
    DBGrid1 OnColEnter
    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
    procedure TForm1.DBGrid1ColEnter(Sender: TObject);
    var
      ColIndex: Integer;
    begin
      if TDBGridLandGreen(DBGrid1).Col <= 0 then
        Exit;
     
      ColIndex := TDBGridLandGreen(DBGrid1).Col - 1;
     
      // Vérifiez que l'indice est valide
      if (ColIndex >= 0) and (ColIndex < DBGrid1.Columns.Count) then
      begin
        if DBGrid1.Columns[ColIndex].Title.Caption = 'Actions' then
          DBGrid1.Options := DBGrid1.Options - [dgEditing]
        else
          DBGrid1.Options := DBGrid1.Options + [dgEditing];
      end;
    end;
    DBGrid1 OnCellClick
    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
    procedure TForm1.DBGrid1CellClick(Column: TColumn);
    var
      pt: TPoint;
      GridCoord: TGridCoord;
      CellRect: TRect;
      IconSpacing, IconWidth, IconLeft, IconTop: Integer;
      IconRect: TRect;
    begin
      if Column.Title.Caption <> 'Actions' then
        Exit;
     
      // Récupérer la position de la souris dans les coordonnées client du DBGrid
      pt := DBGrid1.ScreenToClient(Mouse.CursorPos);
      GridCoord := DBGrid1.MouseCoord(pt.X, pt.Y);
     
      // Obtenir le rectangle de la cellule en cours
      CellRect := TDBGridLandGreen(DBGrid1).CellRect(GridCoord.X, GridCoord.Y);
     
      // Calcul des coordonnées de l'icône :
      IconSpacing := 4;
      IconWidth := ImageList1.Width;
      IconTop := CellRect.Top + (CellRect.Bottom - CellRect.Top - ImageList1.Height) div 2;
      IconLeft := CellRect.Left + IconSpacing;
     
      // Première icône (par exemple, décrémentation)
      IconRect := Rect(IconLeft, IconTop, IconLeft + IconWidth, IconTop + ImageList1.Height);
      if PtInRect(IconRect, pt) then
      begin
        ShowMessage('Icône décrémentation cliquée');
        Exit;
      end;
     
      // Deuxième icône (par exemple, incrémentation)
      IconLeft := IconLeft + IconWidth + IconSpacing;
      IconRect := Rect(IconLeft, IconTop, IconLeft + IconWidth, IconTop + ImageList1.Height);
      if PtInRect(IconRect, pt) then
      begin
        ShowMessage('Icône incrémentation cliquée');
        Exit;
      end;
     
      // Etc................................
    end;
    Nom : Video_2025_06_09-1_edit_0.gif
Affichages : 373
Taille : 471,6 Ko



    Pense à publier tes questions dans les sous-forums (Composants VCL)

  3. #3
    Membre expérimenté
    Avatar de XeGregory
    Homme Profil pro
    Passionné par la programmation
    Inscrit en
    Janvier 2017
    Messages
    506
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Passionné par la programmation
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Janvier 2017
    Messages : 506
    Par défaut
    Citation Envoyé par XeGregory Voir le message
    Bonjour LandGreen


    De cette façon, c'est tout simplement :

    Type TDBGridLandGreen class(TDBGrid)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    type
      TDBGridLandGreen = class(TDBGrid)
      end;
     
    TForm1 = class(TForm)
    // ...
    DBGrid1 OnDrawColumnCell
    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
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer;
      Column: TColumn; State: TGridDrawState);
    var
      IconLeft: Integer;
      IconTop: Integer;
      IconSpacing: Integer;
      IconWidth: Integer;
    begin
      if Column.Title.Caption = 'Actions' then
      begin
        IconSpacing := 4;
        IconWidth := ImageList1.Width;
        IconTop := Rect.Top + (Rect.Bottom - Rect.Top - ImageList1.Height) div 2;
        IconLeft := Rect.Left + IconSpacing;
     
        // Dessiner l'icône de décrémentation (index 0 de l'image list)
        ImageList1.Draw(DBGrid1.Canvas, IconLeft, IconTop, 0);
        Inc(IconLeft, IconWidth + IconSpacing);
     
        // Dessiner l'icône d'incrémentation (index 1 de l'image list)
        ImageList1.Draw(DBGrid1.Canvas, IconLeft, IconTop, 1);
      end
      else
        // Pour toutes les autres colonnes, on laisse le dessin par défaut
        DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
    end;
    DBGrid1 OnColEnter
    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
    procedure TForm1.DBGrid1ColEnter(Sender: TObject);
    var
      ColIndex: Integer;
    begin
      if TDBGridLandGreen(DBGrid1).Col <= 0 then
        Exit;
     
      ColIndex := TDBGridLandGreen(DBGrid1).Col - 1;
     
      // Vérifiez que l'indice est valide
      if (ColIndex >= 0) and (ColIndex < DBGrid1.Columns.Count) then
      begin
        if DBGrid1.Columns[ColIndex].Title.Caption = 'Actions' then
          DBGrid1.Options := DBGrid1.Options - [dgEditing]
        else
          DBGrid1.Options := DBGrid1.Options + [dgEditing];
      end;
    end;
    DBGrid1 OnCellClick
    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
    procedure TForm1.DBGrid1CellClick(Column: TColumn);
    var
      pt: TPoint;
      GridCoord: TGridCoord;
      CellRect: TRect;
      IconSpacing, IconWidth, IconLeft, IconTop: Integer;
      IconRect: TRect;
    begin
      if Column.Title.Caption <> 'Actions' then
        Exit;
     
      // Récupérer la position de la souris dans les coordonnées client du DBGrid
      pt := DBGrid1.ScreenToClient(Mouse.CursorPos);
      GridCoord := DBGrid1.MouseCoord(pt.X, pt.Y);
     
      // Obtenir le rectangle de la cellule en cours
      CellRect := TDBGridLandGreen(DBGrid1).CellRect(GridCoord.X, GridCoord.Y);
     
      // Calcul des coordonnées de l'icône :
      IconSpacing := 4;
      IconWidth := ImageList1.Width;
      IconTop := CellRect.Top + (CellRect.Bottom - CellRect.Top - ImageList1.Height) div 2;
      IconLeft := CellRect.Left + IconSpacing;
     
      // Première icône (par exemple, décrémentation)
      IconRect := Rect(IconLeft, IconTop, IconLeft + IconWidth, IconTop + ImageList1.Height);
      if PtInRect(IconRect, pt) then
      begin
        ShowMessage('Icône décrémentation cliquée');
        Exit;
      end;
     
      // Deuxième icône (par exemple, incrémentation)
      IconLeft := IconLeft + IconWidth + IconSpacing;
      IconRect := Rect(IconLeft, IconTop, IconLeft + IconWidth, IconTop + ImageList1.Height);
      if PtInRect(IconRect, pt) then
      begin
        ShowMessage('Icône incrémentation cliquée');
        Exit;
      end;
     
      // Etc................................
    end;
    Nom : Video_2025_06_09-1_edit_0.gif
Affichages : 373
Taille : 471,6 Ko



    Pense à publier tes questions dans les sous-forums (Composants VCL)
    ?

  4. #4
    Membre très actif
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2018
    Messages
    482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2018
    Messages : 482
    Par défaut
    Citation Envoyé par XeGregory Voir le message
    ?
    XeGregory le génie
    merci

  5. #5
    Membre très actif
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2018
    Messages
    482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2018
    Messages : 482
    Par défaut problème apparu
    Bonjour à tous,

    Je reviens vers vous car je rencontre un problème avec un comportement inattendu. Lorsque je clique deux fois de suite sur une icône, celle-ci disparaît et un curseur apparaît, comme si un champ de saisie s’activait à la place.

    J’ai suivi à la lettre la solution proposée par XeGregory, mais ça ne règle pas le souci.

    Est-ce que quelqu’un aurait une idée ou pourrait m’aider à comprendre d’où ça vient ?

    Merci d’avance pour votre aide !
    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
     
    procedure TF_venterapide002.DBGrid1ColEnter(Sender: TObject);
    var
      ColIndex: Integer;
    begin
      if TDBGridCustom(DBGrid1).Col <= 0 then
        Exit;
     
      ColIndex := TDBGridCustom(DBGrid1).Col - 1;
     
      // Vérifiez que l'indice est valide
      if (ColIndex >= 0) and (ColIndex < DBGrid1.Columns.Count) then
      begin
        if DBGrid1.Columns[ColIndex].Title.Caption = 'Actions' then
          DBGrid1.Options := DBGrid1.Options - [dgEditing]
        else
          DBGrid1.Options := DBGrid1.Options + [dgEditing];
      end;
     
     
    end;
    Images attachées Images attachées  

  6. #6
    Membre très actif
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2018
    Messages
    482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2018
    Messages : 482
    Par défaut
    autres évenement du Dbrgid1 :

    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
    procedure TF_venterapide002.DBGrid1DblClick(Sender: TObject);
    var
      pt: TPoint;
      GridCoord: TGridCoord;
      ColumnIndex: Integer;
    begin
      // Récupérer la position de la souris
      pt := DBGrid1.ScreenToClient(Mouse.CursorPos);
      GridCoord := DBGrid1.MouseCoord(pt.X, pt.Y);
      ColumnIndex := GridCoord.X - 1; // Indice colonne
     
      if (ColumnIndex >= 0) and (ColumnIndex < DBGrid1.Columns.Count) then
      begin
        if DBGrid1.Columns[ColumnIndex].Title.Caption = 'Actions' then
        begin
          // Ignorer le double clic dans la colonne Actions (pour ne pas activer l'édition)
          Exit;
        end
        else
        begin
          // Pour les autres colonnes, activer l'édition normalement
          dbgrid1.ReadOnly := false;
          DBGrid1.Options := DBGrid1.Options + [dgEditing];
          DBGrid1.Options := DBGrid1.Options - [dgRowselect];
        end;
      end;
     
    end;

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

Discussions similaires

  1. calcul de moyenne sur plusieurs plages dans une colonne
    Par Jean-luc79 dans le forum Excel
    Réponses: 2
    Dernier message: 19/12/2014, 11h20
  2. Insérer des données sur plusieurs lignes dans une seule en SQL
    Par nathantahiti dans le forum Développement
    Réponses: 1
    Dernier message: 03/08/2011, 10h47
  3. Gestion des homonymes sur plusieurs champs
    Par riete dans le forum Requêtes
    Réponses: 2
    Dernier message: 31/01/2008, 17h34
  4. Texte sur plusieurs lignes dans une cellule de JTable
    Par JeanECN dans le forum Composants
    Réponses: 3
    Dernier message: 10/04/2006, 17h20
  5. Réponses: 4
    Dernier message: 25/11/2005, 18h15

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