delphi循环遍历同类控件或所有控件
循环遍历同类控件:
form1中有groupbox1,内有多个speedbutton,控制其同时按下。
var i:integer;
begin
for i:=0 to form1.groupbox1.controlcount-1 do
if form1.groupbox1.controls[i] is tspeedbutton then
begin
tspeedbutton(form1.groupbox1.controls[i]).allowallup:=true;
tspeedbutton(form1.groupbox1.controls[i]).groupindex:=i+1;
tspeedbutton(form1.groupbox1.controls[i]).down:=true;
end;
结合数据库使用,表示表rz中的rz字段(bit),为1则按下,为0则抬起。
var i:integer;
begin
i:=0;
while not adotable1.eof then
if adotable1.fields[1].asboolen=true then
begin
tspeedbutton(form1.groupbox1.controls[i]).allowallup:=true;
tspeedbutton(form1.groupbox1.controls[i]).groupindex:=i+1;
tspeedbutton(form1.groupbox1.controls[i]).down:=true;
end
else
begin
tspeedbutton(form1.groupbox1.controls[i]).allowallup:=true;
tspeedbutton(form1.groupbox1.controls[i]).groupindex:=i+1;
tspeedbutton(form1.groupbox1.controls[i]).down:=false;
end;
inc(i);
adotable1.next;
end;
遍历Panel组件上的所有控件:
var i:integer;
begin
for i:=0 to panel5.ControlCount -1 do
begin
if panel5.Controls[i] is TEdit then //Edit组件
begin
((panel5.Controls[i]) as TEdit).Text:='';
((panel5.Controls[i]) as TEdit).Enabled:=true;
end else if panel5.Controls[i] is TComboBox then //ComboBox组件
begin
((panel5.Controls[i]) as TComboBox).Text:='';
((panel5.Controls[i]) as TComboBox).Enabled:=true;
end else if panel5.Controls[i] is TDateTimePicker then //DateTimePicker组件
begin
((panel5.Controls[i]) as TDateTimePicker).Enabled:=true;
((panel5.Controls[i]) as TDateTimePicker).DateTime:=now();
end;
end;
end;
这个过程主要是清空panel5组件Edit、ComboBox组件Text内容、把DateTimePicker组件日期设为当前日期,并使panel5所有控件可用 ......