Petit probleme: j'ai une fiche avec un stringgrid contenant des boutons, dans un premier temps la barre vertical fonctionnait mal
j'ai trouvé en mettant un Stringgrid1TopLeftChanged dans mon stringgrid
mais ce que je ne comprend pas c'est pourquoi dans cette exemple la ligne entre accolades n'affiche pas correctement les boutons {R := StringGrid1.CellRect(ACol, ARow) ... {}
alors que la ligne R := StringGrid1.CellRect(ACol, 0); ... fonctionne
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
 
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, StdCtrls;
 
type
  TForm1 = class(TForm)
    Stringgrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure Stringgrid1TopLeftChanged(Sender: TObject);
private
  public
    { Déclarations publiques }
  end;
 
var
  Form1: TForm1;
  FPrevTopRow: Integer;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.FormCreate(Sender: TObject);
var
  R: TRect;
  I,ACol,Arow: Integer;
  Bouton: TButton;
begin
   StringGrid1.RowCount:=14;
   FPrevTopRow := 0;
   for ACol := 0 to StringGrid1.colCount - 1 do
   begin
     R := StringGrid1.CellRect(ACol, 0); inc(R.Left,StringGrid1.GridLineWidth); inc(R.top,StringGrid1.GridLineWidth);
     for Arow := 0 to StringGrid1.RowCount - 1 do
     begin
{     R := StringGrid1.CellRect(ACol, arow); inc(R.Left,StringGrid1.GridLineWidth); inc(R.top,StringGrid1.GridLineWidth);{}
      Bouton := TButton.Create(StringGrid1);
      I:=Acol+arow*StringGrid1.colCount;
      Bouton.BoundsRect := R;
      Bouton.Top := R.Top;
      Bouton.Left := R.Left;
      Bouton.Height := R.Bottom - R.Top;
      Bouton.Width := R.Right - R.Left;
      Bouton.Caption:=format('[%u,%u]:%u]',[R.Left,R.top,I]);
      Bouton.Tag :=I;
//    Bouton.OnClick := Boutonclick;
      Bouton.Parent := StringGrid1;
//    StringGrid1.Objects[Acol,Arow] := Bouton;
      OffsetRect(R, 0, StringGrid1.DefaultRowHeight + StringGrid1.GridLineWidth);
     end;
  end;
end;
 
procedure TForm1.Stringgrid1TopLeftChanged(Sender: TObject);
var I: Integer;
  Shift: Integer;
  S:TStringGrid;
begin
  S:=(Sender as TStringGrid);
  Shift := (S.TopRow - FPrevTopRow) * (S.DefaultRowHeight + S.GridLineWidth);
  for I := 0 to S.ControlCount - 1 do S.Controls[I].Top := S.Controls[I].Top - Shift;
  FPrevTopRow := S.TopRow;
end;
 
end.
la fiche:
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
 
object Form1: TForm1
  Left = 350
  Top = 110
  Width = 344
  Height = 154
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Stringgrid1: TStringGrid
    Left = 16
    Top = 16
    Width = 300
    Height = 85
    Color = clBtnFace
    ColCount = 3
    DefaultColWidth = 93
    DefaultRowHeight = 16
    FixedCols = 0
    RowCount = 14
    FixedRows = 0
    GridLineWidth = 0
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goRowSizing, goColSizing, goThumbTracking]
    ScrollBars = ssVertical
    TabOrder = 0
    OnTopLeftChanged = Stringgrid1TopLeftChanged
  end
end
j'aurai voulu faire ceci dans une seule boucle for i:=0 to StringGrid1.colCount*StringGrid1.rowCount-1; et R:= StringGrid1.CellRect(I mod StringGrid1.colCount,I div StringGrid1.colCount);
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.FormCreate(Sender: TObject);
var
  R: TRect;
  I,ACol,Arow: Integer;
  Bouton: TButton;
begin
   StringGrid1.RowCount:=14;
   FPrevTopRow := 0;
   for i:=0 to StringGrid1.colCount*StringGrid1.rowCount-1 do
   begin
   R:= StringGrid1.CellRect(I mod StringGrid1.colCount,I div StringGrid1.colCount); inc(R.Left,StringGrid1.GridLineWidth); inc(R.top,StringGrid1.GridLineWidth);{}
      Bouton := TButton.Create(StringGrid1);
      Bouton.BoundsRect := R;
      Bouton.Top := R.Top;
      Bouton.Left := R.Left;
      Bouton.Height := R.Bottom - R.Top;
      Bouton.Width := R.Right - R.Left;
      Bouton.Caption:=format('[%u,%u]:%u]',[R.Left,R.top,I]);
      Bouton.Tag := I;
//    Bouton.OnClick := Boutonclick;
      Bouton.Parent := StringGrid1;
//    StringGrid1.Objects[Acol,Arow] := Bouton;
      OffsetRect(R, 0, StringGrid1.DefaultRowHeight + StringGrid1.GridLineWidth);
     end;
end;
si qqu'un a une idée. le premier code fonctionne Merci de vos réponses (écrit en delphi 7)