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 :

Combobox dans un StringGrid


Sujet :

Composants VCL Delphi

  1. #1
    Membre éprouvé Avatar de BuzzLeclaire
    Homme Profil pro
    Dev/For/Vte/Ass
    Inscrit en
    Août 2008
    Messages
    1 606
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Dev/For/Vte/Ass

    Informations forums :
    Inscription : Août 2008
    Messages : 1 606
    Points : 1 113
    Points
    1 113
    Par défaut Combobox dans un StringGrid
    Bonjour à tous,

    J'essai d'utiliser un combobox à l'intérieur d'un stringrrid, grace à ce projet

    unit Unit1;

    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
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Grids, StdCtrls;
     
    type
      TForm1 = class(TForm)
        StringGrid1: TStringGrid;
        ComboBox1: TComboBox;
        procedure FormCreate(Sender: TObject);
        procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
        procedure ComboBox1Change(Sender: TObject);
        procedure StringGrid1TopLeftChanged(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
     
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      StringGrid1.DefaultRowHeight := ComboBox1.Height;
    end;
     
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
        R: TRect;
    begin
     
      if (TStringGrid(Sender).Cells[2,aRow] <> '') And
        (aCol = 3) and
        (aRow >= StringGrid1.FixedRows) and
        (gdFocused in State) THEN
        with ComboBox1 do
        begin
          BringToFront;
          CopyRect(R, Rect);
          R.TopLeft :=     Form1.ScreenToClient(
                           StringGrid1.ClientToScreen(R.TopLeft));
          R.BottomRight := Form1.ScreenToClient(
                           StringGrid1.ClientToScreen(R.BottomRight));
          SetBounds(R.Left, R.Top, R.Right-R.Left, R.Bottom-R.Top);
        end;
    end;
     
    procedure TForm1.ComboBox1Change(Sender: TObject);
    begin
      with StringGrid1 do
        Cells[Col, Row] := ComboBox1.Text;
    end;
     
    procedure TForm1.StringGrid1TopLeftChanged(Sender: TObject);
    var
      R: TRect;
    begin
      with StringGrid1 do
          CopyRect(R, CellRect(Col, Row));
     
      with ComboBox1 do
      begin
        Visible := False;
        R.TopLeft :=     Form1.ScreenToClient(
                         StringGrid1.ClientToScreen(R.TopLeft));
        R.BottomRight := Form1.ScreenToClient(
                         StringGrid1.ClientToScreen(R.BottomRight));
        SetBounds(R.Left, R.Top, R.Right - R.Left, R.Bottom - R.Top);
      end;
     
      with StringGrid1 do
        if (TopRow <= Row) and (TopRow + VisibleRowCount > Row) then
           ComboBox1.Show;
    end;
     
    end.
    Le probleme que je rencontre, est que si je tape du text en colonne 2 alors le combo s'affiche en colonne trois au clique, mais si par mégarde je donne le focus à une autre cellule de mon StringGrid et que je selectionne un items de mon combo qui pourtant est dans la colonne 3 la cellule devient tout bleu et reçois les infos de mon combo.

    Comment puis-je eviter cela ?

  2. #2
    Membre régulier
    Profil pro
    Inscrit en
    Février 2004
    Messages
    251
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2004
    Messages : 251
    Points : 118
    Points
    118
    Par défaut

    Salut,

    j'ai trouvé un coposant faisant cela :
    http://www.vclcomponents.com/Delphi/...grid-info.html

    A+

    windows 10 / DEBIAN 7.9 / Etc...

  3. #3
    Membre éprouvé Avatar de BuzzLeclaire
    Homme Profil pro
    Dev/For/Vte/Ass
    Inscrit en
    Août 2008
    Messages
    1 606
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Dev/For/Vte/Ass

    Informations forums :
    Inscription : Août 2008
    Messages : 1 606
    Points : 1 113
    Points
    1 113
    Par défaut
    Merci CharleLéo,

    J'aimerais comprendre ma proposition pourquoi j'ai ce phénomène.

    Si quelqu'un peux m'aiguiller merci.

  4. #4
    Expert confirmé
    Avatar de anapurna
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    3 419
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Mai 2002
    Messages : 3 419
    Points : 5 818
    Points
    5 818
    Par défaut
    salut

    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
     
      TForm1 = class(TForm)
        StrGrd : TStringGrid;
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
      private
        PickList : TStringList;
      Public 
         procedure OnGetEditStyle(TSender: TObject; ACol,ARow: Integer; var EditStyle: TEditStyle);
         procedure OnGetPickListItems(ACol, ARow: Integer;Items: TStrings);
         procedure OnEditButtonClick(Sender: TObject);
      end;
     
    var
      Form1: TForm1;
     
    implementation 
     
     procedure TForm1.FormCreate(Sender: TObject);
    var
     x,y : integer;
     str : String;
    begin
      // on creer notre liste
      PickList := TStringList.Create;
      // on remplit la liste
      for x:=0 to pred(planning.colCount) do
        PickList.Add(Format('Item %d', [x]));
     
      // on assigned différent évènement
      StrGrd.OnEditButtonClick   := OnEditButtonClick;
      StrGrd.OnGetPickListItems := OnGetPickListItems;
      StrGrd.OnGetEditStyle      := OnGetEditStyle;
    end;
     
    procedure TForm1.OnGetEditStyle(TSender: TObject;
      ACol,ARow: Integer; var EditStyle: TEditStyle);
    begin
      if ACol= 6 then
        EditStyle := esPickList;
      if ACol= 7 then
        EditStyle := esEllipsis;
      if ACol= 8 then
        EditStyle := esSimple;
    end;
     
    procedure TForm1.OnGetPickListItems(ACol, ARow: Integer;
      Items: TStrings);
    begin
      Items.Assign(PickList);
    end;
     
    procedure TForm1.OnEditButtonClick(Sender: TObject);
    var
      s : string;
    begin
      with StrGrd do
        Cells[Col,Row] := Inputbox('Enter your data', 'Data:', Cells[Col,Row]);
    end;
     
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
      PickList.Free;
    end;
    @+ Phil
    Nous souhaitons la vérité et nous trouvons qu'incertitude. [...]
    Nous sommes incapables de ne pas souhaiter la vérité et le bonheur, et sommes incapables ni de certitude ni de bonheur.
    Blaise Pascal
    PS : n'oubliez pas le tag

  5. #5
    Membre éprouvé Avatar de BuzzLeclaire
    Homme Profil pro
    Dev/For/Vte/Ass
    Inscrit en
    Août 2008
    Messages
    1 606
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Dev/For/Vte/Ass

    Informations forums :
    Inscription : Août 2008
    Messages : 1 606
    Points : 1 113
    Points
    1 113
    Par défaut
    Salut

    Merci anapurna pour ton exemple, malheureusement je ne peux pas l'exploiter il est trop personnalisé.

    Tanpis je laisse tombé je change de méthode.

    Merci

  6. #6
    Expert confirmé
    Avatar de anapurna
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    3 419
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Mai 2002
    Messages : 3 419
    Points : 5 818
    Points
    5 818
    Par défaut
    salut

    en quoi est t'il trop personnalisé ?
    qu'elle est ta demande exacte peut peut on le généraliser pour qu'il colle a
    ta demande

    @+ Phil
    Nous souhaitons la vérité et nous trouvons qu'incertitude. [...]
    Nous sommes incapables de ne pas souhaiter la vérité et le bonheur, et sommes incapables ni de certitude ni de bonheur.
    Blaise Pascal
    PS : n'oubliez pas le tag

  7. #7
    Membre éprouvé Avatar de BuzzLeclaire
    Homme Profil pro
    Dev/For/Vte/Ass
    Inscrit en
    Août 2008
    Messages
    1 606
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Dev/For/Vte/Ass

    Informations forums :
    Inscription : Août 2008
    Messages : 1 606
    Points : 1 113
    Points
    1 113
    Par défaut
    Merci de le proposer

    Je ne peux pas faire cela

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
      // on assigned différent évènement
      StrGrd.OnEditButtonClick   := OnEditButtonClick;
      StrGrd.OnGetPickListItems := OnGetPickListItems;
      StrGrd.OnGetEditStyle      := OnGetEditStyle;
    Il n'existe pas d'evenemtn de la sorte dans un StringGrid

    donc j'ai pas pu tester ta proposition.

    ce que je recherche c'est de faire marcher un combo dans un stringgrid cela je c'est le faire,
    mais si je choisi un item et que le focus se trouve ailleur dans le StringGrid je récupére le text de l'item dans une autre cellule.


  8. #8
    Rédacteur/Modérateur
    Avatar de Andnotor
    Inscrit en
    Septembre 2008
    Messages
    5 692
    Détails du profil
    Informations personnelles :
    Localisation : Autre

    Informations forums :
    Inscription : Septembre 2008
    Messages : 5 692
    Points : 13 122
    Points
    13 122
    Par défaut
    OnDrawCell n'est pas le bon événement.
    Le test doit être fait à l'entrée dans une cellule, OnSelectCell:

    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
    procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
    var
      R :TRect;
    begin
      with TStringGrid(Sender) do
      begin
        Combobox1.Visible := (aCol = 3) and (Cells[2, aRow] <> '');
     
        if Combobox1.Visible then
        begin
          R := CellRect(aCol, aRow);
          OffsetRect(R, Left +2, Top +2);
          Combobox1.BoundsRect := R;
          Combobox1.ItemIndex  := Combobox1.Items.IndexOf(Cells[aCol, aRow]);
        end;
      end;
    end;

  9. #9
    Expert confirmé
    Avatar de anapurna
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    3 419
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Mai 2002
    Messages : 3 419
    Points : 5 818
    Points
    5 818
    Par défaut
    salut

    arf c'est pas bien compliquer
    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
     
     TStringGrid = Class(Grids.TStringGrid)
      ...
      Private
        FDropdownRowCount   : integer;
        FOnEditButtonClick  : TNotifyEvent;
        FOnGetEditStyle     : TGetEditStyleEvent;
        FOnGetPickListItems : TOnGetPickListItems;
        procedure SetDropdownRowCount(value:integer);
        procedure SetOnEditButtonClick(value:TNotifyEvent);
        procedure SetOnGetPicklistItems(value:TOnGetPickListItems);
      protected
        function  CreateEditor: TInplaceEdit; override;
        function  GetEditStyle(ACol, ARow: integer): TEditStyle; override;
      public
        constructor Create(AOwner: TComponent); override;
     published
        property DropdownRowCount   : integer read FDropDownRowCount write SetDropdownRowCount default 8;
        property OnEditButtonClick  : TNotifyEvent read FOnEditButtonClick write SetOnEditButtonClick;
        property OnGetEditStyle     : TGetEditStyleEvent  read FOnGetEditStyle write FOnGetEditStyle;
        property OnGetPickListItems : TOnGetPickListItems read FOnGetPickListItems write SetOnGetPickListItems;
      end;
     
    ...
    Constructor  TStringGrid.Create;
    begin
      inherited Create(AOwner);
      FDropdownRowCount := 8;
    end;
     
    function TStringGrid.GetEditStyle(ACol,ARow:integer) : TEditStyle;
    begin
      result := esSimple;
      if Assigned(FOnGetEditStyle) then
        FOnGetEditStyle(self, ACol, ARow, result);
      Invalidate;
    end;
     
    procedure TStringGrid.SetDropDownRowCount(value:integer);
    begin
      FDropdownRowCount := value;
      if Assigned(InplaceEditor) then
        TInplaceEditList(InplaceEditor).DropdownRows := value;
      Invalidate;
    end;
     
    procedure TStringGrid.SetOnEditButtonClick(value:TNotifyEvent);
    begin
      FOnEditButtonClick := value;
      if Assigned(InplaceEditor) then
        TInplaceEditList(InplaceEditor).OnEditButtonClick := value;
      Invalidate;
    end;
     
    procedure TStringGrid.SetOnGetPicklistItems(value:TOnGetPicklistItems);
    begin
      FOnGetPicklistItems := value;
      if Assigned(InplaceEditor) then
        TInplaceEditList(InplaceEditor).OnGetPickListitems := value;
      Invalidate;
    end;
    @+ Phil
    Nous souhaitons la vérité et nous trouvons qu'incertitude. [...]
    Nous sommes incapables de ne pas souhaiter la vérité et le bonheur, et sommes incapables ni de certitude ni de bonheur.
    Blaise Pascal
    PS : n'oubliez pas le tag

  10. #10
    Membre émérite
    Homme Profil pro
    Directeur technique
    Inscrit en
    Mai 2008
    Messages
    2 401
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Directeur technique
    Secteur : Service public

    Informations forums :
    Inscription : Mai 2008
    Messages : 2 401
    Points : 2 304
    Points
    2 304
    Par défaut
    Salut;

    anapurna est-ce bien légal ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    TStringGrid = Class(Grids.TStringGrid)
    ????
    Bon courage ou Bonne Chance (selon le contexte)
    Mon blog sur WordPress

  11. #11
    Expert éminent
    Avatar de Lung
    Profil pro
    Analyste-programmeur
    Inscrit en
    Mai 2002
    Messages
    2 664
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Analyste-programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2002
    Messages : 2 664
    Points : 6 967
    Points
    6 967
    Par défaut
    Citation Envoyé par Just-Soft Voir le message
    Salut;

    anapurna est-ce bien légal ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    TStringGrid = Class(Grids.TStringGrid)
    ????
    Pourquoi ça ne le serait pas ?
    L'urgent est fait, l'impossible est en cours, pour les miracles prévoir un délai. ___ Écrivez dans un français correct !!

    C++Builder 5 - Delphi 6#2 Entreprise - Delphi 2007 Entreprise - Delphi 2010 Architecte - Delphi XE Entreprise - Delphi XE7 Entreprise - Delphi 10 Entreprise - Delphi 10.3.2 Entreprise - Delphi 10.4.2 Entreprise - Delphi 11.1 Entreprise
    OpenGL 2.1 - Oracle 10g - Paradox - Interbase (XE) - PostgreSQL (15.4)

  12. #12
    Expert confirmé
    Avatar de anapurna
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    3 419
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Mai 2002
    Messages : 3 419
    Points : 5 818
    Points
    5 818
    Par défaut
    salut

    c'est légal si tu veut pas te prendre la tête a créer un nouveau composant
    cela permet de poser ton composant d'origine et de lui appliquer les nouvelle propriété sans avoir a installer un nouveau composant

    c'est le meilleur moyen de tester ton nouveau composant une fois finalise tu peut le mettre dans un package

    @+ Phil
    Nous souhaitons la vérité et nous trouvons qu'incertitude. [...]
    Nous sommes incapables de ne pas souhaiter la vérité et le bonheur, et sommes incapables ni de certitude ni de bonheur.
    Blaise Pascal
    PS : n'oubliez pas le tag

  13. #13
    Membre chevronné Avatar de chaplin
    Profil pro
    Inscrit en
    Août 2006
    Messages
    1 215
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 1 215
    Points : 1 819
    Points
    1 819
    Par défaut
    Je me suis bien pris la tête dessus, mais l'exemple était pédagogique. Il y a quand même quelques subtilités avant d'y arriver.

    EDIT:

    En tout cas, ça m'a servi de base pour comprendre le composant TXStringGrid. Le problème en Delphi 2009 (idem 2007), c'est qu'il faut un peu trafiquer les fichiers pour le faire tourner (ie SourceForge =<D7), le résultat est pas trop mal, ça n'exclu pas quelque plantage .

  14. #14
    Membre éprouvé Avatar de BuzzLeclaire
    Homme Profil pro
    Dev/For/Vte/Ass
    Inscrit en
    Août 2008
    Messages
    1 606
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Dev/For/Vte/Ass

    Informations forums :
    Inscription : Août 2008
    Messages : 1 606
    Points : 1 113
    Points
    1 113
    Par défaut
    Bien,

    J'ai donc testé les 2 propositions.
    La moindre des choses est de vous répondre.

    Anapurna, malheureusement je n'arrive pas a faire fonctionner ta proposition toujours des problèmes dans la déclaration, là j'ai une erreure sur cette ligne :
    FOnGetEditStyle : TGetEditStyleEvent;
    Dommage car j'aurais aimé voir comment cela aurait marché

    AndNotOr, effecivement l'évenement Selectcell marche beaucoup mieux, je l'avais d'ailleur trouvé, sauf que ma méthode etait un peu tirer par les cheveux. En ajoutant à ta proposition combobox1.BrintToFront cela fonctionne trés bien.

    Je vous remercie à tous les 2.

    Merci aux programmeurs qui se penchent sur nos soucis.

    PS : Pour moi je note comme résolu.
    PS Anapurna : Si t'avais un .pas entier peut-être que j'y arriverais lol...

  15. #15
    Expert confirmé
    Avatar de anapurna
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    3 419
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Mai 2002
    Messages : 3 419
    Points : 5 818
    Points
    5 818
    Par défaut
    salut

    tester tel quel

    une simple form avec 9 colonne
    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
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
     
    unit UTestEdit;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ComCtrls, Grids ;
     
    type
     
      TGetEditStyleEvent = procedure (TSender:TObject; ACol,ARow:integer; var EditStyle:TEditStyle) of object;
     
      TStringGrid = Class(Grids.TStringGrid)
      Private
        FDropdownRowCount   : integer;
        FOnEditButtonClick  : TNotifyEvent;
        FOnGetEditStyle     : TGetEditStyleEvent;
        FOnGetPickListItems : TOnGetPickListItems;
        procedure SetDropdownRowCount(value:integer);
        procedure SetOnEditButtonClick(value:TNotifyEvent);
        procedure SetOnGetPicklistItems(value:TOnGetPickListItems);
      protected
        function  CreateEditor: TInplaceEdit; override;
        function  GetEditStyle(ACol, ARow: integer): TEditStyle; override;
      public
        constructor Create(AOwner: TComponent); override;
      published
        property DropdownRowCount   : integer read FDropDownRowCount write SetDropdownRowCount default 8;
        property OnEditButtonClick  : TNotifyEvent read FOnEditButtonClick write SetOnEditButtonClick;
        property OnGetEditStyle     : TGetEditStyleEvent  read FOnGetEditStyle write FOnGetEditStyle;
        property OnGetPickListItems : TOnGetPickListItems read FOnGetPickListItems write SetOnGetPickListItems;
      end;
     
      TForm1 = class(TForm)
        planning: TStringGrid;
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
      private
        PickList : TStringList;
      public
        procedure OnGetEditStyle(TSender: TObject; ACol,ARow: Integer; var EditStyle: TEditStyle);
        procedure OnGetPickListItems(ACol, ARow: Integer;Items: TStrings);
        procedure OnEditButtonClick(Sender: TObject);
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
     
    {$R *.dfm}
     
    Constructor  TStringGrid.Create;
    begin
      inherited Create(AOwner);
      FDropdownRowCount := 8;
    end;
     
    function TStringGrid.CreateEditor: TInplaceEdit;
    begin
      result := TInplaceEditList.Create(self);
      with TInplaceEditList(result) do
      begin
        DropdownRows := FDropdownRowCount;
        OnGetPickListItems := FOnGetPickListItems;
        OnEditButtonClick := FOnEditButtonClick;
      end;
    end;
     
     
    function TStringGrid.GetEditStyle(ACol,ARow:integer) : TEditStyle;
    begin
      result := esSimple;
      if Assigned(FOnGetEditStyle) then
        FOnGetEditStyle(self, ACol, ARow, result);
      Invalidate;
    end;
     
    procedure TStringGrid.SetDropDownRowCount(value:integer);
    begin
      FDropdownRowCount := value;
      if Assigned(InplaceEditor) then
        TInplaceEditList(InplaceEditor).DropdownRows := value;
      Invalidate;
    end;
     
    procedure TStringGrid.SetOnEditButtonClick(value:TNotifyEvent);
    begin
      FOnEditButtonClick := value;
      if Assigned(InplaceEditor) then
        TInplaceEditList(InplaceEditor).OnEditButtonClick := value;
      Invalidate;
    end;
     
    procedure TStringGrid.SetOnGetPicklistItems(value:TOnGetPicklistItems);
    begin
      FOnGetPicklistItems := value;
      if Assigned(InplaceEditor) then
        TInplaceEditList(InplaceEditor).OnGetPickListitems := value;
      Invalidate;
    end;
     
     
    procedure TForm1.FormCreate(Sender: TObject);
    var
     x : integer;
    begin
      PickList := TStringList.Create;
      for x:=0 to pred(planning.colCount) do
        PickList.Add(Format('Item %d', [x]));
     
      planning.OnEditButtonClick  := OnEditButtonClick;
      planning.OnGetPickListItems := OnGetPickListItems;
      planning.OnGetEditStyle := OnGetEditStyle;
    end;
     
    procedure TForm1.OnGetEditStyle(TSender: TObject;
      ACol,ARow: Integer; var EditStyle: TEditStyle);
    begin
      if ACol= 6 then
        EditStyle := esPickList;
      if ACol= 7 then
        EditStyle := esEllipsis;
      if ACol= 8 then
        EditStyle := esSimple;
    end;
     
    procedure TForm1.OnGetPickListItems(ACol, ARow: Integer;
      Items: TStrings);
    begin
      Items.Assign(PickList);
    end;
     
    procedure TForm1.OnEditButtonClick(Sender: TObject);
    begin
      with planning do
        Cells[Col,Row] := Inputbox('Enter your data', 'Data:', Cells[Col,Row]);
    end;
     
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
      PickList.Free;
    end;
     
    end.
    @+ Phil
    Nous souhaitons la vérité et nous trouvons qu'incertitude. [...]
    Nous sommes incapables de ne pas souhaiter la vérité et le bonheur, et sommes incapables ni de certitude ni de bonheur.
    Blaise Pascal
    PS : n'oubliez pas le tag

  16. #16
    Membre éprouvé Avatar de BuzzLeclaire
    Homme Profil pro
    Dev/For/Vte/Ass
    Inscrit en
    Août 2008
    Messages
    1 606
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Dev/For/Vte/Ass

    Informations forums :
    Inscription : Août 2008
    Messages : 1 606
    Points : 1 113
    Points
    1 113
    Par défaut
    Merci anapurna.

    La ca fonctionne bien.

    Que dire sa semble parfait, le coup du ellipsis cela fait vraiment bien.
    Ta proposition dépasse largement mon besoin mais il y réponds parfaitement.

    Tu t'es surpassé sur ce coup là.

    Merci.

  17. #17
    Membre chevronné Avatar de chaplin
    Profil pro
    Inscrit en
    Août 2006
    Messages
    1 215
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 1 215
    Points : 1 819
    Points
    1 819
    Par défaut
    En faisant copier coller du code d'Anapurna, en Delphi 2009, le TStringGrid ne réagit absolument pas. C'est pour ça que je dis que je me suis pris la tête dessus. J'aimerais savoir si d'autres possesseurs de Delphi 2009 ont testé le code. Je suis parvenu à le faire fonctionné, mais après quelques modifications du code.

  18. #18
    Expert confirmé
    Avatar de anapurna
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    3 419
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Mai 2002
    Messages : 3 419
    Points : 5 818
    Points
    5 818
    Par défaut
    salut

    je n'est pas essayé avec 2009
    c'est quoi ta prise de tête ?

    @+ Phil
    Nous souhaitons la vérité et nous trouvons qu'incertitude. [...]
    Nous sommes incapables de ne pas souhaiter la vérité et le bonheur, et sommes incapables ni de certitude ni de bonheur.
    Blaise Pascal
    PS : n'oubliez pas le tag

  19. #19
    Membre chevronné Avatar de chaplin
    Profil pro
    Inscrit en
    Août 2006
    Messages
    1 215
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 1 215
    Points : 1 819
    Points
    1 819
    Par défaut
    Quand je dis prise de tête, c'est mon expression . En fait, c'est dans la séquence d'instanciation des classes. J'ai du déclarer une instance de l'objet TInplaceEditList en privée dans la classe héritant de TStringGrid. C'est comme si la classe n'était jamais instanciée, du coup avec les assigned en test (toujours false), forcément il n'y a aucun résultat.

    En fait CreateEditor est appelé dans la procédure UpdateEdit, au départ j'avais pas compris qu'il fallait mettre dans les options goEditing et goAlawaysShowEditor à true sinon CanEditShow est à false donc pas de CreateEditor et donc pas de TInplaceEditList instancié. Mais malgré cela, la séquence des actions fait que CreateEditor est appelé en dernier, ce qui m'a conduit à l'instancier lors de la création du TStringgrid.

    EDIT: Pour être plus clair j'ai mis un peu de 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
    procedure TCustomGrid.UpdateEdit;
    ...
    
    begin
      if CanEditShow then
      begin
        if FInplaceEdit = nil then
        begin
          FInplaceEdit := CreateEditor;
          FInplaceEdit.SetGrid(Self);
          FInplaceEdit.Parent := Self;
          UpdateEditor;
        end
    ...
    function TCustomGrid.CanEditShow: Boolean;
    begin
      Result := ([goRowSelect, goEditing] * Options = [goEditing]) and
        FEditorMode and not (csDesigning in ComponentState) and HandleAllocated and
        ((goAlwaysShowEditor in Options) or IsActiveControl);
    end;
    Aurais-je loupé une étape ?

    EDIT 2:

    J'ai trouvé un viel article traitant du sujet, mais en Delphi 2009, il faut revoir la copie.

  20. #20
    Expert confirmé
    Avatar de anapurna
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    3 419
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Mai 2002
    Messages : 3 419
    Points : 5 818
    Points
    5 818
    Par défaut
    salut

    je comprend pas trop puisque dans le createEditor tu creer
    la list
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    function TStringGrid.CreateEditor: TInplaceEdit;
    begin
      result := TInplaceEditList.Create(self);
      with TInplaceEditList(result) do
      begin
        DropdownRows := FDropdownRowCount;
        OnGetPickListItems := FOnGetPickListItems;
        OnEditButtonClick := FOnEditButtonClick;
      end;
    end;
    a bin Tiens voila l'article que j'avais du lire à l'époque je m'en sert un peu différemment mais la solution reste la même tu déclare le nouvel éditeur au moment ou tu le créer
    faut peut etre rajouter un mais normalement il devrait fonctionner dans toutes les version de delphi

    @+ Phil

    PS: je testerai dans la semaine sur un delphi 200...
    Nous souhaitons la vérité et nous trouvons qu'incertitude. [...]
    Nous sommes incapables de ne pas souhaiter la vérité et le bonheur, et sommes incapables ni de certitude ni de bonheur.
    Blaise Pascal
    PS : n'oubliez pas le tag

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. Problème avec ComboBox dans un StringGrid !
    Par petitclem dans le forum C++Builder
    Réponses: 4
    Dernier message: 09/04/2010, 14h20
  2. Ajouter ComboBox dans StringGrid
    Par jojo86 dans le forum Composants VCL
    Réponses: 4
    Dernier message: 11/03/2008, 23h47
  3. des combobox dans un stringgrid
    Par cibi182 dans le forum Composants VCL
    Réponses: 5
    Dernier message: 21/04/2006, 10h00
  4. Comment insérer un ComboBox dans un cellule StringGrid
    Par Xavier dans le forum C++Builder
    Réponses: 4
    Dernier message: 09/01/2006, 11h36
  5. Comment dessiner un ComboBox dans un StringGrid ?
    Par gilles641 dans le forum Composants VCL
    Réponses: 8
    Dernier message: 30/07/2005, 15h19

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