cast TObject using his ClassType?

前端 未结 5 1239
抹茶落季
抹茶落季 2021-01-13 07:27

how can i make my code to work ? :) i`ve tried to formulate this question but after several failed attempts i think you guys will spot the problem faster looking at the code

5条回答
  •  余生分开走
    2021-01-13 08:02

    It would be easier to use RTTI instead of explicit casting, ie:

    uses
      TypInfo;
    
    setCtrlState([ memo1, edit1, button1], False);
    
    procedure setCtrlState(objs: array of TObject; bState: boolean = True);
    var
      obj: TObject;
      PropInfo: PPropInfo;
    begin
      for obj in objs do
      begin
        PropInfo := GetPropInfo(obj, 'ReadOnly');
        if PropInfo <> nil then SetOrdProp(obj, PropInfo, not bState);
    
        PropInfo := GetPropInfo(obj, 'Enabled');
        if PropInfo <> nil then SetOrdProp(obj, PropInfo, bState);
      end;
    end;
    

提交回复
热议问题