Comment mettre (inclure) une combobox dans un stringgrid avec création dynamique de composant

Contenu du snippet

J'ai trouvé ce source sur un site portugais (remerciements à l'auteur). Je l'ai modifié afin qu'il traite sans buggs la possibilité d'avoir une comboBox (mais quand on a compris pour le ComboBox, ça fonctionne aussi avec d'autres composants) dans la cellule sélectionnée d'uns StringGrid.

Créez une fiche, mettez y une StringGrid dedans et c'est tout. La ComboBox et son évènement OnExit est créé dynamiquement.

C'est très simple en fait ...

Source / Exemple :


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure StringGrid1SelectCell(Sender: TObject; Col, Row: Integer;
      var CanSelect: Boolean);
    procedure FormShow(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
    procedure ComboBox1Exit(Sender: TObject);
  end;

var
  Form1: TForm1;
  ComboBox1:TComboBox;
  
implementation

{$R *.dfm}

procedure TForm1.ComboBox1Exit(Sender: TObject);
begin
     StringGrid1.Cells[StringGrid1.Col,StringGrid1.Row]:=ComboBox1.Text;
     ComboBox1.Visible := False;
     StringGrid1.SetFocus;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject;Col,Row: Integer; var CanSelect: Boolean);
var
   R: TRect;
begin
     if (Col > 0)
     and (Row <> 0)
     then begin
          R := StringGrid1.CellRect(Col, Row);
          R.Left := R.Left + StringGrid1.Left;
          R.Right := R.Right + StringGrid1.Left;
          R.Top := R.Top + StringGrid1.Top;
          R.Bottom := R.Bottom + StringGrid1.Top;
          ComboBox1.Left := R.Left + 1;
          ComboBox1.Top := R.Top + 1;
          ComboBox1.Width := (R.Right + 1) - R.Left;
          ComboBox1.Height := (R.Bottom + 1) - R.Top;
          ComboBox1.Visible := True;
          if StringGrid1.Cells[Col,Row]<>''
          then ComboBox1.Text:=StringGrid1.Cells[Col,Row]
          else ComboBox1.Text:='';
          ComboBox1.SetFocus;
     end;
     CanSelect := True;
end;

procedure TForm1.FormShow(Sender: TObject);
Var
   X:Integer;
begin
     StringGrid1.Options:=StringGrid1.Options+[goColSizing,goThumbTracking]-[goEditing];
     ComboBox1:=TComboBox.Create(Self);
     ComboBox1.Parent:=Form1;
     ComboBox1.Visible:=False;
     ComboBox1.OnExit:=ComboBox1Exit;

     ComboBox1.Clear;
     For X:=1 to 10
     do ComboBox1.Items.Add(IntToStr(X));

end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
     ComboBox1.Free;
end;

end.

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.