===================================
本人log中所有未注明转载的文章和blog一般为本人原创或整理加工,
原创文章版权本人(lonefox)所有;转载文章版权归原作者所有;
http://blog.youkuaiyun.com/boythl
欢迎转载,但请注明出处,保留作者和版权信息。
===================================
几个月前因为项目需要,本人扩展了标准TStringGrid组件,并命名为TTXStringGrid; 这是TTXStringGrid的升级版V1.1.64; 经过本人一段时间测试,消灭了大部分bug。
主要增加对单元格个性化设置的支持;接口为TTXStringGrid的CellConfigs属性或CellConfig[ACol, ARow]属性,但请注意,单元格设置只支持运行时设置;不支持design-time修改(想想一个1000行*100列的表格,设计时显示该属性的话。。。);TTXStringGrid增加了几个事件。
所谓的单元格个性化设置和列的个性化设置功能相同。当一个单元格既有自己的特定设置,它所属列也有特定设置时,将采用该单元格的特定设置,而不采用所属列的特定设置。
使用文档目前没有时间整理。有问题请留言,lonefox保证第一时间回复。
本来打算实现如同Excel中的冻结列功能; delphi的类层次给我痛头一击, TCustomDrawGrid类中很多该开放的函数不允许重载,若想实现该功能,基本上得放弃TCustomDrawGrid的大部分函数,自己再实现一次;只好放弃。
1.1.64版下载地址:https://p-blog.youkuaiyun.com/images/p_blog_youkuaiyun.com/boythl/EntryImages/20081125/TXStringGridV1.1.64.jpg 下载后将扩展名修改为RAR即可,完整的源码包。因源码较长,不再在下文罗列。
1.0版发布地址:http://blog.youkuaiyun.com/boythl/archive/2008/10/07/3026533.aspx
下版本开发目标:1.支持更多种Editor。2.冻结列(就目前的继承组系谱来讲有点难度哦)。
修改历史如下:
History:
V1.0
Class TTXStringGrid:
1.[Add Columns],
which can set/get columns of the grid as the same as TDBGrid
V1.1
Class TTXStringGrid:
1.[add property CellConfig],
through it,we can set/get special cells different from the parent column
2.[add property CellConfigs],
it's a collection of CellConfig.
3.[delete event OnColEnter],
we havn't implement it.
4.[delete event OnColExit],
we havn't implement it.
5.[Add event OnCellExit],
fired when the focus changed, select another cell or lost focus.
Class TTXStringGridInplaceEdit
1.[hide property DataList]
Hide this property, because we don't support it in TTXColumns;
2.[Modify method CloseUp]
Modify it, so we can modify the cell's text with drop-down list
even if its ReadOnly is true.
Class TTXColumn
1.[add property OnPickListCloseUp]
Fired when user close the drop-down list inside the column.
New Class TTXCellConfig
1.[add property OnPickListCloseUp]
Fired when user close the drop-down list inside a cell. Note, when it's fired,
the OnPickListCloseUp event of its owner column won't be fired anymore.
Bug corrected:
Class TTXStringGrid:
1.[modify property Columns],
if the TTXStringGrid.ColCount is changed, the Columns.Count isn't updated.
2.[modify method Create]
Create default columns without BeginUpdate and EndUpdate, which is wrong.
3.[modify method SetColCount]
In it, around Columns.Add and Columns.delete, must has BeginUpdate and EndUpdate.
4.[delete private member FTitleOffset]
For solving the problem of the grid always draw a title,
Replace all FTitleOffset with FixedRows; Deleted the parameter FTitleOffset.
Class TColumnTitle:
1.[Add method Assign]
To solve sometimes the IDE messegeboxs "Can't assign a TColumnTitle to a TColumnTitle".
Class TColumn:
1.[modify method SetPickList]
Havn't make sure FPickList not nil before call Assign.
下面是一个动态创建Grid的简单实例,例子未包含所有新增功能:
- procedure TForm1.RzButton1Click(Sender: TObject);
- var sg:TTXStringGrid; v : TStringList;
- begin
- sg:=TTXStringGrid.Create(self);
- sg.Options := sg.Options + [goEditing, goRowMoving, goColMoving, goAlwaysShowEditor]; //使表格始终显示Editor
- sg.Columns[1].PickList := self.Memo1.Lines; //下列列表内容,
- sg.Columns[1].ButtonStyle := tcbsAutoSelect;//第二列的类型,必须设置PickList属性
- sg.Columns[3].PickList := self.Memo2.Lines; //第四列的列表内容
- sg.Columns[3].ButtonStyle := tcbsAutoSelect;
- sg.Columns[3].ReadOnly := True; //第四列只读,这里就只能用下拉列表选
- sg.Parent := self; //动态创建时必须指定Parent
- sg.Top := 0;
- sg.Left := 0;
- sg.visible := true;
- sg.CellConfig[2, 3].Color := clRed; //第3列第4行的单元格显示红色背景色
- sg.CellConfig[3, 1].ButtonStyle := tcAutoSelect;
- sg.CellConfig[3, 1].PickList := self.Memo3.Lines; //第四列第二行显示一个不同的下拉列表
- sg.CellConfig[3, 1].DropDownRows := 3; //下拉列表只显示3行
- sg.CellConfigs.Item[2, 1] := sg.CellConfig[2, 3]; //将第三列第四列的设置同样的赋给第三列第二行
- sg.Columns[4].ReadOnly := True; //第五列只读
- sg.CellConfig[4 , 1].ReadOnly := False; //第五列第二行将可读写
- sg.RowCount := 8;
- sg.CellConfig[1 , 5].ButtonStyle := tcAutoSelect;
- sg.CellConfig[1 , 2].PickList := Nil; //第二列第三行的cell将不显示下拉列表,替换列设置效果
- sg.CellConfig[1 , 3].ReadOnly := True;
- sg.RowCount := 5;
- sg.RowCount := 8;
- sg.Width := 1000;
- sg.Height := 800;
- v := TStringList.Create;
- v.Add('1');
- v.Add('2');
- v.Add('3');
- v.Add('14');
- v.Add('5');
- sg.Columns[1].PickList := v;
- with sg.Columns[2] do begin
- PickList := v;
- DropDownRows := 15;
- ButtonStyle := tcbsAutoSelect;
- end;
- end;