delphi中,遍历窗体上的控件,并显示在ListBox1中:
procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
begin
ListBox1.Items.Clear;
ListBox1.Items.Add('开始遍历控件******');
for i:=1 to ComponentCount-1 do
begin
ListBox1.Items.Add(Components[i].Name);
if Components[i] is TDateTimePicker then
ListBox1.Items.Add('时间控件值:' + DateToStr(TDateTimePicker(Components[i]).Date));
if Components[i] is Tedit then
ListBox1.Items.Add('文本框值:'+Tedit(Components[i]).text);
end;
ListBox1.Items.Add('遍历控件结束******');
end;
遍历GroupBox1中的子控件(和窗体中的控件不同)
procedure TForm1.Button2Click(Sender: TObject);
var
i:Integer;
begin
ListBox1.Items.Clear;
ListBox1.Items.Add('开始遍历控件******');
for i:=1 to GroupBox1.ControlCount-1 do
begin
ListBox1.Items.Add(GroupBox1.Controls[i].Name);
if GroupBox1.Controls[i] is TDateTimePicker then
ListBox1.Items.Add('时间控件值:' + DateToStr(TDateTimePicker(GroupBox1.Controls[i]).Date));
if GroupBox1.Controls[i] is Tedit then
ListBox1.Items.Add('文本框值:'+Tedit(GroupBox1.Controls[i]).text);
end;
ListBox1.Items.Add('遍历控件结束******');
end;