下面代码不能通过,原因可能:temp是tComponent 不存在enabled 和readonly等属性.请问要怎样写,才能实现:查找窗体里有没有button1控件,有就将它的enabled属性设为false.
var
I: Integer;
Temp: TComponent;
begin
for I := ComponentCount - 1 downto 0 do
begin
Temp := Components[I];
if (Temp is Tbutton) and (Temp.Name='DBGrid1') then
Temp.enabled:=false;
if (Temp is TEdit) and (Temp.Name='Edit1') then
Temp.readonly:=true;
end;
end;
var
I: Integer;
Temp: TComponent;
begin
for I := ComponentCount - 1 downto 0 do
begin
Temp := Components[I];
if (Temp is Tbutton) and (Temp.Name='DBGrid1') then
Temp.enabled:=false;
if (Temp is TEdit) and (Temp.Name='Edit1') then
Temp.readonly:=true;
end;
end;
if Self.Components[i].ClassType=TButton then
Tbutton(Self.Components[i]).Enabled := False;
begin
if components[i].Name='Button1' then //注意Name要区分大小写
end;
end;
至于在不在Panel里没关系
i :integer;
begin
for i := 0 to ComponentCount-1 do
begin
if (Components[i] is TButton) and (Components[i].Name='Button1') then
Controls[i].Enabled := False;
if (Components[i] is TEdit) and (Components[i].Name = 'Edit1') then
TEdit(Components[i]).ReadOnly := True;
end;
end;
i:integer;
begin
for i:= 0 to self.ComponentCount - 1 do
if (self.Components[i] is TButton) then
if (self.Components[i] as TButton).Caption = 'Button1' then
(self.Components[i] as TButton).Enabled := False;
end;
上面的就可以了
for i := 0 to ComponentCount-1 do
begin
if (Components[i] is TButton) and (Components[i].Name='Button1') then
Controls[i].Enabled := False;
end;
end;
把这句 if (self.Components[i] as TButton).Caption = 'Button1' then
改为if (self.Components[i] as TButton).Name = 'Button1' then
就OK
self.Components[i] as TButton就跟Button1一样
if Form1.Components[Index] is TEdit then
(Form1.Components[Index] as TEdit).Color:=ClLime;
if Self.Components[i].ClassType=TButton then
Tbutton(Self.Components[i]).Enabled := False;