一个多 TTabSheet 的界面,
符合某种条件下,
就是希望某一个选中的 TTabSheet 界面里面的容器 Tpanel 里所有的 TDBEdit 或者 TDBComboBox 之类的,只读。有什么快捷的方法吗?很多,如果一个一个写,那是需要很多时间啊。
比如,下面的这一段函数好像不起作用,是为什么呢?procedure Tform_Pre.SetState(AIndex: Integer; ViewState: Boolean);
var
  i: Integer;
  tsCur: TTabSheet;
begin
  tsCur := pcSta.Pages[AIndex];
  if ViewState then
  begin
    for i := 0 to tsCur.ControlCount - 1 do
      if tsCur.Controls[i] is TDBEdit then
      begin
        TDBEdit(tsCur.Controls[i]).ReadOnly := True;
        TDBEdit(tsCur.Controls[i]).Color := ViewColor;
      end
      else if tsCur.Controls[i] is TDBComboBox then
      begin
        TDBComboBox(tsCur.Controls[i]).ReadOnly := True;
        TDBComboBox(tsCur.Controls[i]).Color := ViewColor;
      end
      else if tsCur.Controls[i] is TDBDateTimeEditEh then
      begin
        TDBDateTimeEditEh(tsCur.Controls[i]).ReadOnly := True;
        TDBDateTimeEditEh(tsCur.Controls[i]).Color := ViewColor;
      end
      else if tsCur.Controls[i] is TDBMemo then
      begin
        TDBMemo(tsCur.Controls[i]).ReadOnly := True;
        TDBMemo(tsCur.Controls[i]).Color := ViewColor;
      end;
  end
  else
    for i := 0 to tsCur.ControlCount - 1 do
      if tsCur.Controls[i] is TDBEdit then
      begin
        TDBEdit(tsCur.Controls[i]).ReadOnly := False;
        TDBEdit(tsCur.Controls[i]).Color := clWindow;
      end
      else if tsCur.Controls[i] is TDBComboBox then
      begin
        TDBComboBox(tsCur.Controls[i]).ReadOnly := False;
        TDBComboBox(tsCur.Controls[i]).Color := clWindow;
      end
      else if tsCur.Controls[i] is TDBDateTimeEditEh then
      begin
        TDBDateTimeEditEh(tsCur.Controls[i]).ReadOnly := False;
        TDBDateTimeEditEh(tsCur.Controls[i]).Color := clWindow;
      end
      else if tsCur.Controls[i] is TDBMemo then
      begin
        TDBMemo(tsCur.Controls[i]).ReadOnly := False;
        TDBMemo(tsCur.Controls[i]).Color := clWindow;
      end;  end;
end;

解决方案 »

  1.   

    楼主的代码不是已经实现了吗或者在 panel里判断:for(i:=0;i<panel.Controls.Count;i++)
    begin
      if( panel.Controls[i] is TDBEdit) then
       panel.Controls[i].Enabled := false;
    end;
      

  2.   

    在一个unit里写:
      Procedure GroupBox_ReadOnly_True(GroupBoxName:TGroupBox);//将GroupBox内项目设置成只读
      Procedure GroupBox_ReadOnly_False(GroupBoxName:TGroupBox);//将GroupBox内项目设置成可写
      Procedure GroupBox_Clear(GroupBoxName:TGroupBox);//将GroupBox内项目清空
      Procedure GroupBox_Color(GroupBoxName:TGroupBox;StrColor:TColor); //将GroupBox内项目变颜色
    Procedure GroupBox_ReadOnly_True(GroupBoxName:TGroupBox);
    //引用 Controls, StdCtrls
    var
      i:integer;
    begin
      with GroupBoxName do
        for i:=0 to controlcount-1 do
        begin
          if controls[i] is TEdit     then TEdit(controls[i]).ReadOnly := True;
          if controls[i] is TEdit     then TEdit(controls[i]).Color := clWindow;
          if controls[i] is TComboBox then TComboBox(controls[i]).Enabled:=False;
          if controls[i] is TComboBox then TComboBox(controls[i]).Color:=clWindow;
          if controls[i] is TCheckBox then TCheckBox(controls[i]).Enabled:=False;
          if controls[i] is TDateTimePicker then TDateTimePicker(controls[i]).Enabled:=False;
          if controls[i] is TMemo     then TMemo(controls[i]).ReadOnly := True;
          if controls[i] is TMemo     then TMemo(controls[i]).Color := clWindow;
        end;
    end;Procedure GroupBox_ReadOnly_False(GroupBoxName:TGroupBox);
    //引用Windows, System
    var
      i:integer;
    begin
      with GroupBoxName do
        for i:=0 to controlcount-1 do
        begin
          if controls[i] is TEdit     then TEdit(controls[i]).ReadOnly := False;
          if controls[i] is TEdit     then TEdit(controls[i]).Color := clwindow;
          if controls[i] is TComboBox then TComboBox(controls[i]).Enabled:=True;
          if controls[i] is TComboBox then TComboBox(controls[i]).Color:=clwindow;
          if controls[i] is TCheckBox then TCheckBox(controls[i]).Enabled:=True;
          if controls[i] is TDateTimePicker then TDateTimePicker(controls[i]).Enabled:=True;
          if controls[i] is TMemo     then TMemo(controls[i]).ReadOnly := False;
          if controls[i] is TMemo     then TMemo(controls[i]).Color := clwindow;
        end;
    end;Procedure GroupBox_Clear(GroupBoxName:TGroupBox);//将GroupBox内项目清空
    //引用Windows, System
    var
      i:integer;
    begin
      with GroupBoxName do
        for i:=0 to controlcount-1 do
        begin
          if controls[i] is TEdit     then TEdit(controls[i]).Clear;
          if controls[i] is TComboBox then TComboBox(controls[i]).Text:='';
        end;
    end;Procedure GroupBox_Color(GroupBoxName:TGroupBox;StrColor:TColor);
    //引用Windows, System
    var
      i:integer;
    begin
      with GroupBoxName do
        for i:=0 to controlcount-1 do
        begin
          if controls[i] is TEdit     then TEdit(controls[i]).Font.Color := StrColor;
          if controls[i] is TComboBox then TComboBox(controls[i]).Font.Color:=StrColor;
          if controls[i] is TDateTimePicker then TDateTimePicker(controls[i]).Font.Color:=StrColor;
        end;
    end;然后在你的form里调用就可以了
      

  3.   

    我曾经是这么干的,效果也不错
    Procedure TFrm_Pub.SetComponentReadOnly(ParentComponent:TWincontrol;bReadOnly:boolean);
    var I:integer;
    begin
      for I:=0 to  ParentComponent.ControlCount-1 do
      begin    if  ParentComponent.Controls[i] is TEdit then
        begin
          TEdit(ParentComponent.Controls[i]).ReadOnly:=bReadOnly;
          if bReadOnly then
          TEdit(ParentComponent.Controls[i]).Color:=clBtnFace else
          TEdit(ParentComponent.Controls[i]).Color:=clWindow;
        end;    if  ParentComponent.Controls[i] is TDBEdit then
        begin
          TDBEdit(ParentComponent.Controls[i]).ReadOnly:=bReadOnly;
          if bReadOnly then
          TDBEdit(ParentComponent.Controls[i]).Color:=clBtnFace else
          TDBEdit(ParentComponent.Controls[i]).Color:=clWindow;
        end;    if  ParentComponent.Controls[i] is TRZDBCombobox then
        begin
          TRZDBCombobox(ParentComponent.Controls[i]).ReadOnly:=bReadOnly;
          if bReadOnly then
          TRZDBCombobox(ParentComponent.Controls[i]).Color:=clBtnFace else
          TRZDBCombobox(ParentComponent.Controls[i]).Color:=clWindow;
        end;    if  ParentComponent.Controls[i] is TDBCombobox then
        begin
          TDBCombobox(ParentComponent.Controls[i]).ReadOnly:=bReadOnly;
          if bReadOnly then
          TDBCombobox(ParentComponent.Controls[i]).Color:=clBtnFace else
          TDBCombobox(ParentComponent.Controls[i]).Color:=clWindow;
        end;    if  ParentComponent.Controls[i] is TRZDBEdit then
        begin
          TRZDBEdit(ParentComponent.Controls[i]).ReadOnly:=bReadOnly;
          if bReadOnly then
          TRZDBEdit(ParentComponent.Controls[i]).Color:=clBtnFace else
          TRZDBEdit(ParentComponent.Controls[i]).Color:=clWindow;
        end;    if  ParentComponent.Controls[i] is TDBDateTimeEditEh then
        begin
          TDBDateTimeEditEh(ParentComponent.Controls[i]).ReadOnly:=bReadOnly;
          if bReadOnly then
          TDBDateTimeEditEh(ParentComponent.Controls[i]).Color:=clBtnFace else
          TDBDateTimeEditEh(ParentComponent.Controls[i]).Color:=clWindow;
        end;
      end;
    end;
      

  4.   

    这个最好的办法是运用RTTI进行设置
     for i := 0 to ComponentCount - 1 do
      begin
        if GetPropInfo(Components[i], 'ReadOnly') <> nil then
        begin
          SetPropValue(Components[i], 'ReadOnly',true)
        end;
      end;