我的一个过程是这样写的
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');请高手指点!!!

解决方案 »

  1.   

    如果dlephi里面有宏替换之类的东西就好了, 
    或者你改成case语句看看!!!也许比if要好一点 !
      

  2.   

    ????有没有不需要用宏替换的方法?如:procedure SetCtrlColor(ctrl:TWinControl;ctrlType:string)
    begin
       (ctrl as Class(ctrlType)).Font.Color:= clRed;
    end当然这只是构想,我还不知道具体的实现办法。
      

  3.   

    首先先引用TypInfo单元
    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;
      

  4.   

    如果调用时知道控件的类型,那么你可以:
    procedure SetCtrlColor(ctrl:TWinControl;ctrlType:string)
    begin
       if (ctrl.ClassName = ctrlType) then   //最好用字符比较函数
       begin
         :
         :
       end;
    end
      

  5.   

    谢谢jacky_shen(jacky) ,你给出的代码基本上解决了我的问题。我还要测试才可。也谢谢xz1980(xz1980),你给了我一个优化现有程序的办法。我还想听听还有没有其他人有另外的解决办法,谢谢!