我的一个过程是这样写的
procedure SetCtrlColor(ctrl:TWinControl)
begin
if ctrl is TEdit then
(ctrl as TEdit).Font.Color:=clRed
else if ctrl is TComboBox then
(ctrl as TComboBox).Font.Color:=clRed
else if ctrl is TDBEdit then
...
...
...
end但我觉得这样不好,一个原因是代码麻烦,如果种类一多,我的重复的语句就不得不往下写下去;另一个原因是执行效率低每对一个控件进行操作,就要对做完所有判断,如果我的判断有100次,那就要判断100次。所以我想改造这个过程:procedure SetCtrlColor(ctrl:TWinControl;ctrlType:string)
begin
(ctrl as 这个地方不知道如何写).Font.Color:= clRed;
end调用时一个例子如下:
SetCtrlColor(myCtrl,'TDBLookupComboBox');请高手指点!!!
procedure SetCtrlColor(ctrl:TWinControl)
begin
if ctrl is TEdit then
(ctrl as TEdit).Font.Color:=clRed
else if ctrl is TComboBox then
(ctrl as TComboBox).Font.Color:=clRed
else if ctrl is TDBEdit then
...
...
...
end但我觉得这样不好,一个原因是代码麻烦,如果种类一多,我的重复的语句就不得不往下写下去;另一个原因是执行效率低每对一个控件进行操作,就要对做完所有判断,如果我的判断有100次,那就要判断100次。所以我想改造这个过程:procedure SetCtrlColor(ctrl:TWinControl;ctrlType:string)
begin
(ctrl as 这个地方不知道如何写).Font.Color:= clRed;
end调用时一个例子如下:
SetCtrlColor(myCtrl,'TDBLookupComboBox');请高手指点!!!
解决方案 »
- 在B包里为什么不能调用Aqn里的方法
- Delphi中Hook的一个简单问题
- 新手请教:我在做一个日记本,就跟QQ空间一样,但用SQL该怎么存储日记啊??
- 我建了一个无窗体的程序,怎样才能让这个程序运行在后台不退出?
- 如何实现类似网页的frame框架?
- 一个判断数据表是否存在的问题 delphi ADO
- 呵呵,谁截过DELPHI启动时的图片?我用PRINT SCREEN截不到哦
- 请问学完了基本的控件和编程以后,是不是该找一本数据库开发的的书比着例子做几个程序.我不知道在往哪方面发展了.请各位高手多多指教小弟.
- 谁能给我介绍一下api函数的参数!!
- 怎样让Treeview的所有项(包括所有子项)在程序启动就完全展开?
- 三层结构的问题!急急!
- 问题??问题??控件问题??
或者你改成case语句看看!!!也许比if要好一点 !
begin
(ctrl as Class(ctrlType)).Font.Color:= clRed;
end当然这只是构想,我还不知道具体的实现办法。
uses
TypInfo;定义一个用于类型转换的类
type
TMyControl = class(TControl)
public
property Font;
end;这里是一个设置具有Font属性的控件的Font属性的颜色
procedure SetFontColor(Obj: TControl);
begin
if GetPropInfo(Obj, 'Font') <> nil then
TMyControl(Obj).Font.Color := clRed;
end;使用方法:
procedure TForm1.Button2Click(Sender: TObject);
begin
SetFontColor(Label1);
SetFontColor(ComboBox1);
SetFontColor(Memo1);
SetFontColor(CheckBox1);
end;
procedure SetCtrlColor(ctrl:TWinControl;ctrlType:string)
begin
if (ctrl.ClassName = ctrlType) then //最好用字符比较函数
begin
:
:
end;
end