for i := 0 to self.ComponentCount - 1 do begin if (self.Components[i] is TDBEdit) then TDBEdit(Self.Components[i]).ReadOnly := True; end;Self也可以换成你的DBEdit所在的Panel
呵, 用递归class procedure TDBFunction.SetDBControlReadOnly(aParentControl: TWinControl; aValue: Boolean); var i: integer; p: PPropInfo; begin for i := 0 to aParentControl.ControlCount - 1 do begin if not (aParentControl.Controls[i] is TWinControl) then Continue; if csAcceptsControls in aParentControl.Controls[i].ControlStyle then SetDBControlReadOnly(TWinControl(aParentControl.Controls[i]), aValue); if aParentControl.Controls[i] is TPageControl then SetDBControlReadOnly(TWinControl(aParentControl.Controls[i]), aValue); p := GetPropInfo(aParentControl.Controls[i].ClassInfo, 'ReadOnly'); if Assigned(p) then SetPropValue(aParentControl.Controls[i], 'ReadOnly', aValue); end; end;
你将要控制的TDBEdit控件取类似的名字: 例如:DBEdtIn1,DBEdtIn2....DBEdtIn50然后: var CanEdit:Booean; begin for I:=1 to 50 do begin TDBEdit(FindComponent('DBEdtIn'+IntTostr(I))).ReadOnly:=not CanEdit; if CanEdit then //还可以控制颜色 TDBEdit(FindComponent('DBEdtIn'+IntTostr(I))).Color:=clWindow else TDBEdit(FindComponent('DBEdtIn'+IntTostr(I))).Color:=clInfoBk; end; end;
if (self.Components[i] is TDBEdit) then
TDBEdit(Self.Components[i]).ReadOnly := True;
end;Self也可以换成你的DBEdit所在的Panel
var
i: integer;
p: PPropInfo;
begin
for i := 0 to aParentControl.ControlCount - 1 do
begin
if not (aParentControl.Controls[i] is TWinControl) then Continue; if csAcceptsControls in aParentControl.Controls[i].ControlStyle then
SetDBControlReadOnly(TWinControl(aParentControl.Controls[i]), aValue); if aParentControl.Controls[i] is TPageControl then
SetDBControlReadOnly(TWinControl(aParentControl.Controls[i]), aValue); p := GetPropInfo(aParentControl.Controls[i].ClassInfo, 'ReadOnly');
if Assigned(p) then
SetPropValue(aParentControl.Controls[i], 'ReadOnly', aValue);
end;
end;
例如:DBEdtIn1,DBEdtIn2....DBEdtIn50然后:
var
CanEdit:Booean;
begin
for I:=1 to 50 do
begin
TDBEdit(FindComponent('DBEdtIn'+IntTostr(I))).ReadOnly:=not CanEdit;
if CanEdit then //还可以控制颜色
TDBEdit(FindComponent('DBEdtIn'+IntTostr(I))).Color:=clWindow
else
TDBEdit(FindComponent('DBEdtIn'+IntTostr(I))).Color:=clInfoBk;
end;
end;