CnPack Forum


 
Subject: 经验交流:D7下为StringGrid实现一个特殊的需求
zjy
管理员
Rank: 9Rank: 9Rank: 9



UID 2
Digest Posts 6
Credits 2385
Posts 1543
点点分 2385
Reading Access 102
Registered 2002-12-16
Location China
Status Offline
Post at 2008-2-21 14:35  Profile | Site | Blog | P.M. 
经验交流:D7下为StringGrid实现一个特殊的需求

最近在工作中使用 TStringGrid 时遇到个比较特殊的需求:要求 Grid 中某些 Cell 只能用鼠标点击来控制其内容(开关切换),而其它的 Cell 则允许用户编辑内容。查看 D7 下的 TStringGrid,没有发现能直接实现该需求的办法,于是分析一下 VCL 源码,并通过以下简单的方法实现了该需求。现帖上来与大家交流。

具体的实现是在使用该 Grid 的窗体单元中,增加以下代码:

  TEditShowEvent = procedure (var CanShow: Boolean) of object;

  // 增加了控制编辑框是否允许事件的 StringGrid
  TStringGrid = class(Grids.TStringGrid)
  private
    FOnEditShow: TEditShowEvent;
  protected
    function CanEditShow: Boolean; override;
  public
    property OnEditShow: TEditShowEvent read FOnEditShow write FOnEditShow;
  end;

implementation

{ TStringGrid }
function TStringGrid.CanEditShow: Boolean;
begin
  Result := inherited CanEditShow;
  if Assigned(FOnEditShow) then
    FOnEditShow(Result);
  if not Result then
    EditorMode := False;
end;

基本思路如下:
因为不希望注册新的 IDE 控件,所以在窗体中使用了同名类覆盖的小技巧,通过声明 TStringGrid = class(Grids.TStringGrid) 在窗体中为 VCL 的 TStringGrid 派生出一个同名的类,但是增加了一个 OnEditShow 事件。新的控件通过 override CanEditShow 方法来产生事件供用户使用。使用时,通过代码的方式手动为该 Grid 关联 OnEditShow 事件并在其中对 Cell 位置进行判断以解决原始需求。

补充:
这个新的 TStringGrid 类是跟 Form 的定义在同一个单元的,针对窗体上已经放置的 TStringGrid 控件有效。之所以使用这种方式,一是不想注册新的 IDE 控件,二是不想手工动态创建新定义的控件。




Zhou JingYu
CnPack Administrator
http://www.cnpack.org/
Top
Passion (LiuXiao)
管理员
Rank: 9Rank: 9Rank: 9


UID 359
Digest Posts 19
Credits 6756
Posts 3554
点点分 6756
Reading Access 102
Registered 2004-3-28
Status Offline
Post at 2008-2-21 23:09  Profile | Blog | P.M. 
TStringGrid = class(Grids.TStringGrid)
这种思想有点儿意思。
Top
jAmEs_
灌水部部长
Rank: 8Rank: 8



Medal No.1  
UID 886
Digest Posts 0
Credits 1134
Posts 600
点点分 1134
Reading Access 10
Registered 2005-6-5
Location 广东
Status Offline
Post at 2008-2-22 09:28  Profile | Blog | P.M. 
的確有意思,想不到有這樣的做法,厲害
Top
bahamut8348
灌水司司长
Rank: 6Rank: 6


UID 4743
Digest Posts 14
Credits 337
Posts 79
点点分 337
Reading Access 10
Registered 2007-1-18
Status Offline
Post at 2008-2-23 13:13  Profile | Blog | P.M. 
就是类的继承吧,Grids.TStringGrid也就是指定是Grids单元的TStringGrid类吧




做人要厚道,看帖要回贴
Top
zjy
管理员
Rank: 9Rank: 9Rank: 9



UID 2
Digest Posts 6
Credits 2385
Posts 1543
点点分 2385
Reading Access 102
Registered 2002-12-16
Location China
Status Offline
Post at 2008-2-25 14:28  Profile | Site | Blog | P.M. 
这个方法也是从朋友那学来的,经常要对标准vcl控件做些修改,又嫌重新注册新控件太麻烦,就用这种方法了,还挺方便,呵呵。




Zhou JingYu
CnPack Administrator
http://www.cnpack.org/
Top
skyjacker
版主
Rank: 7Rank: 7Rank: 7
茶农


UID 2239
Digest Posts 9
Credits 617
Posts 269
点点分 617
Reading Access 100
Registered 2006-6-8
Status Offline
Post at 2008-2-26 08:48  Profile | Blog | P.M.  | QQ
学习ing




一壶清茶煮青春.
Top
kendling (小冬)
高级版主
Rank: 8Rank: 8
MyvNet


Medal No.1  
UID 703
Digest Posts 5
Credits 978
Posts 580
点点分 978
Reading Access 101
Registered 2005-2-18
Location 广东
Status Offline
Post at 2008-3-14 14:52  Profile | Site | Blog | P.M.  | QQ | Yahoo!
还不知道有TStringGrid = class(Grids.TStringGrid)这个方法呢。




小冬
http://MyvNet.com
Top
free9527
新警察
Rank: 1


UID 40579
Digest Posts 0
Credits 2
Posts 1
点点分 2
Reading Access 10
Registered 2008-6-18
Status Offline
Post at 2008-6-18 16:08  Profile | Blog | P.M. 
刚来到这个地方,就学到了知识,仔细记下了,谢谢楼主的奉献!
Top
jjwwang
普通灌水员
Rank: 2



UID 40594
Digest Posts 0
Credits 50
Posts 22
点点分 50
Reading Access 10
Registered 2008-6-22
Status Offline
Post at 2008-6-26 15:33  Profile | Blog | P.M. 
只能是动态创建并使用了吧.
Top
zjy
管理员
Rank: 9Rank: 9Rank: 9



UID 2
Digest Posts 6
Credits 2385
Posts 1543
点点分 2385
Reading Access 102
Registered 2002-12-16
Location China
Status Offline
Post at 2008-6-27 11:11  Profile | Site | Blog | P.M. 


QUOTE:
原帖由 jjwwang 于 2008-6-26 15:33 发表
只能是动态创建并使用了吧.

不只是动态创建使用,可以放在窗体上使用的。之所以不用新的类名而沿用 TStringGrid 的类名,就是为了在窗体上使用。窗体上放的是 vcl 标准的 TStringGrid,但由于单元中定义了新的 TStringGrid,所以编译后运行期窗体创建出来的是新的 TStringGrid 实例,当然扩展出来的属性和事件只能在运行期手工关联了。




Zhou JingYu
CnPack Administrator
http://www.cnpack.org/
Top
 




All times are GMT++8, the time now is 2024-4-19 00:47

    本论坛支付平台由支付宝提供
携手打造安全诚信的交易社区 Powered by Discuz! 5.0.0  © 2001-2006 Comsenz Inc.
Processed in 0.006949 second(s), 7 queries , Gzip enabled

Clear Cookies - Contact Us - CnPack Website - Archiver - WAP