Thema: Delphi Buttons in Stringgrid

Einzelnen Beitrag anzeigen

Peter-Pascal

Registriert seit: 18. Feb 2007
Ort: 32139 Spenge
197 Beiträge
 
Delphi 11 Alexandria
 
#5

AW: Buttons in Stringgrid

  Alt 25. Jul 2011, 20:22
Wenn du nach wie vor auf eigene Schalter bestehst, dann hilft dir vielleicht folgender Code weiter:

Delphi-Quellcode:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    ComboBox1: TComboBox;
    Label1: TLabel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure ComboBox1Change(Sender: TObject);
    procedure StringGrid1TopLeftChanged(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure setRect(rec: TRect); // per Hand eintragen!
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

// Beim Laden des Formulars werden die notwendigen Initialisierungen des StringGrid durchgeführt:
procedure TForm1.FormCreate(Sender: TObject);
begin
 with StringGrid1 do
  begin
   // Spalten:
   ColCount := 8;
   ColWidths[0] := 80; // erste Spalte 80 Pixel breit
   Cells[0,0] := 'Zeitraum';
   Cells[1,0] := 'Sonntag';
   Cells[2,0] := 'Montag';
   Cells[3,0] := 'Dienstag';
   Cells[4,0] := 'Mittwoch';
   Cells[5,0] := 'Donnerstag';
   Cells[6,0] := 'Freitag';
   Cells[7,0] := 'Sonnabend';
   // Zeilen:
   RowCount := 4;
   Cells[0,1] := ' 6:00 - 14:00';
   Cells[0,2] := '14:00 - 22:00';
   Cells[0,3] := '22:00 - 6:00';
   DefaultRowHeight := Combobox1.Height;

   Options := Options + [goTabs];
  end

 end;

// Positionierung der Combobox (muss als Methode von Form1 deklariert werden):
procedure TForm1.setRect(rec: TRect);
var r: TRect;
begin
  with ComboBox1 do
  begin
        CopyRect(r, rec); // rec => r (Eck-Koordinaten an Hilfsrechteck übergeben)
        // Umrechnung der Eck-Koordinaten des Hilfsrechtecks:
        r.BottomRight := Parent.ScreenToClient(StringGrid1.ClientToScreen(r.BottomRight));
        r.TopLeft := Parent.ScreenToClient(StringGrid1.ClientToScreen(r.TopLeft));
        // Positionierung der Combobox (Left, Top, Width und Height werden zugewiesen):
        SetBounds(r.Left, r.Top, r.Right-r.Left, r.Bottom-r.Top);
        BringToFront // Combobox in Vordergrund bringen
   end
end;

// Im OnDrawCell-Event wird Combobox exakt über die Zelle positioniert:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
   if (ACol >= StringGrid1.FixedCols) and (ARow >= StringGrid1.FixedRows) and
      (gdFocused in State) then setRect(Rect);
end;

// Comboboxinhalt wird in Zelle übertragen:
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
   Beep;
   with StringGrid1 do Cells[Col, Row] := ComboBox1.Text
end;

//Der folgende Eventhandler versteckt die Combobox, wenn sie aus dem StringGrid "herausgescrollt" wird:
procedure TForm1.StringGrid1TopLeftChanged(Sender: TObject);
begin
 with StringGrid1 do setRect(CellRect(Col,Row));
end;


end.
das habe ich aus dem sogenannten Kochbuch von Doberenz und Kowalski.

Klappt aber, ich habe es mit eine TColorBox programmiert, sollte auch mit TButton funktionieren
Peter Niemeier
  Mit Zitat antworten Zitat