5,388
社区成员
发帖
与我相关
我的任务
分享
//或者这样写:
procedure CompTxtClear(Ctrl: TControl);
var
I: Integer;
begin
if Ctrl is TEdit then Tedit(Ctrl).Text := '';
if Ctrl is TMemo then TMemo(Ctrl).Text := '';
if Ctrl is TComboBox then
begin
TComboBox(Ctrl).Text := '' ;
TComboBox(Ctrl).ItemIndex:= TComboBox(Ctrl).Items.IndexOf('');
end;
if Ctrl is TWinControl then
for I := 0 to TWinControl(Ctrl).ControlCount - 1 do
CompTxtClear(TWinControl(Ctrl).Controls[I]); //递归调用
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
CompTxtClear(Form1);
end;
procedure CompTxtClear(ParentCtrl: TWinControl);
var
I: Integer;
Ctrl: TWinControl;
begin
for I := 0 to ParentCtrl.ControlCount - 1 do
begin
if ParentCtrl.Controls[I] is TWinControl then
begin
Ctrl := TWinControl(ParentCtrl.Controls[I]);
if Ctrl is TEdit then Tedit(Ctrl).Text:='';
if Ctrl is TMemo then TMemo(Ctrl).Text:='';
if Ctrl is TComboBox then
begin
TComboBox(Ctrl).Text := '' ;
TComboBox(Ctrl).ItemIndex:= TComboBox(Ctrl).Items.IndexOf('');
end;
if (Ctrl.ControlCount > 0) then
CompTxtClear(Ctrl); //递归调用
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
CompTxtClear(Form1)
end;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ComboBox1: TComboBox;
Button1: TButton;
Memo1: TMemo;
Edit1: TEdit;
Edit2: TEdit;
ComboBox2: TComboBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
CompTxtClear(Form1);
end;
end.
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
procedure CompTxtClear(MyFrm:TForm);
implementation
procedure CompTxtClear(MyFrm:TForm);
var i:Integer;
E1:TEdit;
M1:TMemo;
C1:TCombobox;
begin
for i:=0 to MyFrm.ComponentCount - 1 do
begin
if MyFrm.Components[i] is TEdit then
begin
E1 := TEdit(MyFrm.Components[i]);
E1.Text:='';
end;
if MyFrm.Components[i] is TMemo then
begin
M1 := TMemo(MyFrm.Components[i]);
M1.Text:='';
end;
if MyFrm.Components[i] is TCombobox then
begin
C1 := TCombobox(MyFrm.Components[i]);
C1.Text:='';
C1.ItemIndex:=C1.Items.IndexOf('');
end;
end;
end;
end.
procedure CompTxtClear(MyFrm:TForm);
var i:Integer;
E1:TEdit;
M1:TMemo;
C1:TCombobox;
begin
for i:=0 to MyFrm.ComponentCount - 1 do
begin
if MyFrm.Components[i] is TEdit then
begin
E1 := TEdit(MyFrm.Components[i]);
E1.Text:='';
end;
if MyFrm.Components[i] is TMemo then
begin
M1 := TMemo(MyFrm.Components[i]);
M1.Text:='';
end;
if MyFrm.Components[i] is TCombobox then
begin
C1 := TCombobox(MyFrm.Components[i]);
C1.Text:='';
C1.ItemIndex:=C1.Items.IndexOf('');
end;
end;
end;