请教各位大神一个遍历控件的问题,谢谢

changfenglee 2018-12-03 12:27:45
各位大侠:
小弟菜鸟,在这里问个小问题,求大神们点化一下:
下面这张图片是我用遍历控件的方式将控件的文本属性清空,实现没有问题,功能也可以


但因为每个窗体都会用到这个功能,我想写到公共模块中,所以就写成了下面这张图片的样子,但写成这样后功能就不能实现了,请问下问题出在哪里啊



------------------------------------------------------------------------------------------
下面是代码:



//这段代码是写在窗体单元中的,可以实现功能
procedure TFrmMeterielList.CompClear;
var
i:Integer;
begin
for i:=0 to ComponentCount - 1 do
begin
if (Components[i]) is TEdit then
begin
(Components[i] as TEdit).Text:=''
end;

if (Components[i]) is TMemo then
begin
(Components[i] as TMemo).Text:=''
end;

if (Components[i]) is TComboBox then
begin
(Components[i] as TComboBox).Text:='' ;
(Components[i] as TComboBox).ItemIndex:= (Components[i] as TComboBox).Items.IndexOf('');
end;

end;

end;


//下面这段代码是写在公共单元中的,不能实现功能
procedure CompTxtClear(MyForm:TForm);
var
i:Integer;
begin
for i:=0 to MyForm.ComponentCount - 1 do
begin

if MyForm.Components[i] is TEdit then
begin
(MyForm.Components[i] as TMemo).Text:=''
end;

if MyForm.Components[i] is TMemo then
begin
(MyForm.Components[i] as TMemo).Text:=''
end;

if MyForm.Components[i] is TComboBox then
begin
(MyForm.Components[i] as TComboBox).Text:='' ;
(MyForm.Components[i] as TComboBox).ItemIndex:= (MyForm.Components[i] as TComboBox).Items.IndexOf('');
end;

end;

end;
...全文
193 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyhoo163 2018-12-05
  • 打赏
  • 举报
回复
成功就好了。。。。。
changfenglee 2018-12-04
  • 打赏
  • 举报
回复
谢谢大家,已经可以了,就是调用的时候不对,因为我用得是选项卡方式打开窗体,窗体又是动态创建,所以不好传递
MyForm.Parent:=MyTabSheet;
而窗体包含在选项卡中,所以调用的时候,直接传递窗体父级控件,修改一下就可以了

原调用方式为:
CompTextClear(窗体名称);

改成下面这样就可以实现了
CompTextClear(RzPageControl1.ActivePage);


再次感谢这么多大神帮忙,小弟在此谢过了

changfenglee 2018-12-04
  • 打赏
  • 举报
回复
谢谢指点,常规的窗体是可以实现的,但因为现在写得这个窗体打开方式不一样,所以用下面这种方式打开的窗体,还是无法实现

procedure TFrmMain.OpenForm(FrmName, FrmCaption: string;MyFormCls:TFormClass);
var
MyTabSheet:TRzTabSheet;
MyForm:TForm ;
begin
MyTabSheet := TRzTabSheet(self.FindComponent(FrmName));
if MyTabSheet = nil then
begin
try
//创建新标签页
MyTabSheet := TRzTabSheet.Create(self);
MyTabSheet.Name := FrmName;
MyTabSheet.PageControl := RzPageControl1;
MyTabSheet.Caption := FrmCaption;
MyTabSheet.Align := alClient;
try
MyForm:=MyFormCls.Create(Self);
MyForm.Parent:=MyTabSheet;
MyForm.BorderStyle:=bsNone;
MyForm.Align:=alClient;
MyForm.Show;
except
FreeAndNil(MyForm);
Abort;
end;
except
FreeAndNil(MyTabSheet);
Exit;
end;
end;
RzPageControl1.ActivePage:=MyTabSheet;

end;
BlueStorm 2018-12-04
  • 打赏
  • 举报
回复

//或者这样写:
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;
BlueStorm 2018-12-04
  • 打赏
  • 举报
回复

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;
changfenglee 2018-12-04
  • 打赏
  • 举报
回复
引用 10 楼 doloopcn 的回复:
procedure CompTxtClear(MyForm:TForm);
这种传递方式应该是不行的,最简单理想的传递应该是:
procedure CompTxtClear(Sender:TObject);
begin
MyForm:=TForm(Sender);
.............
end;

实际使用中,这种传递还是会出现某些问题,我没去研究。
只有把完整的TYourForm的类正确传递,才不会出现问题


这种方式我也试过,因为窗体打开方式不是一般的这种方法,所以才会出现这个问题,应该像您说的是窗体传递的问题,但一直也没有找到解决方案
doloopcn 2018-12-03
  • 打赏
  • 举报
回复
procedure CompTxtClear(MyForm:TForm);
这种传递方式应该是不行的,最简单理想的传递应该是:
procedure CompTxtClear(Sender:TObject);
begin
MyForm:=TForm(Sender);
.............
end;

实际使用中,这种传递还是会出现某些问题,我没去研究。
只有把完整的TYourForm的类正确传递,才不会出现问题
changfenglee 2018-12-03
  • 打赏
  • 举报
回复
窗体打开是动态传递参数打开的方式,所以窗体打开后,公共模块中的函数如果涉及到窗体的都不行
changfenglee 2018-12-03
  • 打赏
  • 举报
回复
引用 7 楼 lyhoo163 的回复:
提醒参数不能使用 var MyForrm:TForm ,这样在Form中调用它,出现传递身体的错误。


谢谢,的确是可以,但在我这里还是不行,因为我用的是选项卡打开窗体的模式,下面是打开窗体的代码,正因为中间夹了这一层,直接调用就不灵了
procedure TFrmMain.OpenForm(FrmName, FrmCaption: string;MyFormCls:TFormClass);
var
MyTabSheet:TRzTabSheet;
MyForm:TForm ;
begin
MyTabSheet := TRzTabSheet(self.FindComponent(FrmName));
if MyTabSheet = nil then
begin
try
//创建新标签页
MyTabSheet := TRzTabSheet.Create(self);
MyTabSheet.Name := FrmName;
MyTabSheet.PageControl := RzPageControl1;
MyTabSheet.Caption := FrmCaption;
MyTabSheet.Align := alClient;
try
MyForm:=MyFormCls.Create(Self);
MyForm.Parent:=MyTabSheet;
MyForm.BorderStyle:=bsNone;
MyForm.Align:=alClient;
LevelLoadItems(MyForm,qry2);
MyForm.Show;
except
FreeAndNil(MyForm);
Abort;
end;
except
FreeAndNil(MyTabSheet);
Exit;
end;
end;
RzPageControl1.ActivePage:=MyTabSheet;
FrmMain.stat1.Panels[0].Text:='当前操作模块为 【'+ RzPageControl1.ActivePage.Caption+'】';
end;
lyhoo163 2018-12-03
  • 打赏
  • 举报
回复
提醒参数不能使用 var MyForrm:TForm ,这样在Form中调用它,出现传递身体的错误。
lyhoo163 2018-12-03
  • 打赏
  • 举报
回复
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.


代码测试过了,有效。
lyhoo163 2018-12-03
  • 打赏
  • 举报
回复
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;


这样写,比较好。应该有效了。
_码农一个_ 2018-12-03
  • 打赏
  • 举报
回复
procedure CompTxtClear(MyForm:TForm); 改为: procedure CompTxtClear(var MyForm:TForm);
  • 打赏
  • 举报
回复
应该就是#1说的问题
if MyForm.Components[i] is TEdit then
begin
(MyForm.Components[i] as TMemo).Text:=''; // 应该是 (MyForm.Components[i] as TEdit).Text:='';
end;
changfenglee 2018-12-03
  • 打赏
  • 举报
回复
我试了好几种方法都不行,可能是出在MyForm的变量上,不知道是继承问题还是什么
CorinLiu 2018-12-03
  • 打赏
  • 举报
回复
if MyForm.Components[i] is TEdit then begin (MyForm.Components[i] as TMemo).Text:='' //TEdit as TMemo 这个有问题吗? end; 这种问题很简单调试一下就解决吧

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧