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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
unit UTestEdit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, Grids ;
type
TGetEditStyleEvent = procedure (TSender:TObject; ACol,ARow:integer; var EditStyle:TEditStyle) of object;
TStringGrid = Class(Grids.TStringGrid)
Private
FDropdownRowCount : integer;
FOnEditButtonClick : TNotifyEvent;
FOnGetEditStyle : TGetEditStyleEvent;
FOnGetPickListItems : TOnGetPickListItems;
procedure SetDropdownRowCount(value:integer);
procedure SetOnEditButtonClick(value:TNotifyEvent);
procedure SetOnGetPicklistItems(value:TOnGetPickListItems);
protected
function CreateEditor: TInplaceEdit; override;
function GetEditStyle(ACol, ARow: integer): TEditStyle; override;
public
constructor Create(AOwner: TComponent); override;
published
property DropdownRowCount : integer read FDropDownRowCount write SetDropdownRowCount default 8;
property OnEditButtonClick : TNotifyEvent read FOnEditButtonClick write SetOnEditButtonClick;
property OnGetEditStyle : TGetEditStyleEvent read FOnGetEditStyle write FOnGetEditStyle;
property OnGetPickListItems : TOnGetPickListItems read FOnGetPickListItems write SetOnGetPickListItems;
end;
TForm1 = class(TForm)
planning: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
PickList : TStringList;
public
procedure OnGetEditStyle(TSender: TObject; ACol,ARow: Integer; var EditStyle: TEditStyle);
procedure OnGetPickListItems(ACol, ARow: Integer;Items: TStrings);
procedure OnEditButtonClick(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
Constructor TStringGrid.Create;
begin
inherited Create(AOwner);
FDropdownRowCount := 8;
end;
function TStringGrid.CreateEditor: TInplaceEdit;
begin
result := TInplaceEditList.Create(self);
with TInplaceEditList(result) do
begin
DropdownRows := FDropdownRowCount;
OnGetPickListItems := FOnGetPickListItems;
OnEditButtonClick := FOnEditButtonClick;
end;
end;
function TStringGrid.GetEditStyle(ACol,ARow:integer) : TEditStyle;
begin
result := esSimple;
if Assigned(FOnGetEditStyle) then
FOnGetEditStyle(self, ACol, ARow, result);
Invalidate;
end;
procedure TStringGrid.SetDropDownRowCount(value:integer);
begin
FDropdownRowCount := value;
if Assigned(InplaceEditor) then
TInplaceEditList(InplaceEditor).DropdownRows := value;
Invalidate;
end;
procedure TStringGrid.SetOnEditButtonClick(value:TNotifyEvent);
begin
FOnEditButtonClick := value;
if Assigned(InplaceEditor) then
TInplaceEditList(InplaceEditor).OnEditButtonClick := value;
Invalidate;
end;
procedure TStringGrid.SetOnGetPicklistItems(value:TOnGetPicklistItems);
begin
FOnGetPicklistItems := value;
if Assigned(InplaceEditor) then
TInplaceEditList(InplaceEditor).OnGetPickListitems := value;
Invalidate;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
x : integer;
begin
PickList := TStringList.Create;
for x:=0 to pred(planning.colCount) do
PickList.Add(Format('Item %d', [x]));
planning.OnEditButtonClick := OnEditButtonClick;
planning.OnGetPickListItems := OnGetPickListItems;
planning.OnGetEditStyle := OnGetEditStyle;
end;
procedure TForm1.OnGetEditStyle(TSender: TObject;
ACol,ARow: Integer; var EditStyle: TEditStyle);
begin
if ACol= 6 then
EditStyle := esPickList;
if ACol= 7 then
EditStyle := esEllipsis;
if ACol= 8 then
EditStyle := esSimple;
end;
procedure TForm1.OnGetPickListItems(ACol, ARow: Integer;
Items: TStrings);
begin
Items.Assign(PickList);
end;
procedure TForm1.OnEditButtonClick(Sender: TObject);
begin
with planning do
Cells[Col,Row] := Inputbox('Enter your data', 'Data:', Cells[Col,Row]);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
PickList.Free;
end;
end. |
Partager