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 ?