Evo, jos jedan primjer vezan za ovaj slucaj, pa neka se nadje ovdje ako jos nekome zatreba... :)
You can "emulate" checkboxes through:
1. Create new class thats represent cell value. // StringGrid have an Objects property
2. define eventhandler OnClick from StringGrid // calculate cell when user click
3. in CellObject class define draw method
.....
Simple Example:
// on Form only StringGrid1
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
State: TGridDrawState);
procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TCellObj = class
private
_grid : TStringGrid;
_value : boolean;
public
property Value : boolean read _value;
procedure ChangeValue;
procedure Draw(Rect: TRect; State: TGridDrawState);
constructor Create(grid : TStringGrid);
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if Assigned(StringGrid1.Objects[ACol, ARow]) then
(StringGrid1.Objects[ACol, ARow] as TCellObj).Draw(Rect, State);
end;
{ TCellObj }
constructor TCellObj.Create(grid: TStringGrid);
begin
_grid := grid;
_value := false;
end;
procedure TCellObj.Draw(Rect: TRect; State: TGridDrawState);
begin
try
if _value then
DrawFrameControl(_grid.Canvas.Handle, Rect, DFC_BUTTON, DFCS_BUTTONCHECK)
else
DrawFrameControl(_grid.Canvas.Handle, Rect, DFC_BUTTON, DFCS_BUTTONCHECK or DFCS_CHECKED);
except
end;
end;
procedure TCellObj.ChangeValue;
begin
_value := not _value;
end;
procedure TForm1.StringGrid1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var ACol, ARow : integer;
begin
StringGrid1.MouseToCell(X, Y, ACol, ARow);
if Assigned(StringGrid1.Objects[ACol, ARow]) then
(StringGrid1.Objects[ACol, ARow] as TCellObj).ChangeValue;
end;
procedure TForm1.FormCreate(Sender: TObject);
var i, j: integer;
begin
StringGrid1.DefaultDrawing := false;
for i:=1 to StringGrid1.ColCount-1 do
for j := 1 to StringGrid1.RowCount - 1 do
StringGrid1.Objects[i, j] := TCellObj.Create(StringGrid1);
end;
end.
Carpe diem!