delphi2007 教程

delphi2007 教程

首页 新随笔 联系 聚合 管理
  1013 Posts :: 0 Stories :: 28 Comments :: 0 Trackbacks
如何使用FindComponent来调用另一个form中名字有规律的几个TEdit VCL组件开发及应用
http://www.delphi2007.net/DelphiVCL/html/delphi_20061224235716141.html
想在form1中对form2的几个名字有规律的TEdit文本框设置值  
  使用了form2.FindComponent('Edit'+inttostr(i)).text   :=.....;  
  但这样显然是不行的,  
  但又不能写成form2.TEdit.FindComponent('Edit'+inttostr(i)).text   :=.....;  
  改如何写才能得到我想要的功能,使用FindComponent来调用另一个form中名字有规律的几个TEdit  
  望指教,谢谢

楼主的思维是对的,可能是引用的时候没有将被引用的对象进行实例化  
   
  我的测试语句,证明是可行的:  
   
  implementation  
  uses   unit2;  
  {$R   *.dfm}  
   
  procedure   TForm1.Button1Click(Sender:   TObject);  
  var   tmpFrm:   TForm2;  
          I:   Integer;  
  begin  
      tmpFrm   :=   TForm2.Create(self);         //一定要进行实例化  
      for   I   :=   0   to   10   do  
      begin  
          if   tmpFrm.FindComponent('Edit'+IntToStr(I))   <>   nil   then  
              self.Memo1.Lines.Add(TEdit(tmpFrm.FindComponent('Edit'+IntToStr(I))).Text);  
          //Memo1用以测试是否查询成功,如果成功则添加值       仅用于测试显示  
      end;  
      tmpFrm.ShowModal;  
      tmpFrm.Free;  
  end;

TEdit(form2.FindComponent('Edit'+inttostr(i))).text   :=...;

你的form2已经创建了吗?   只有创建了才行啊,  
  如果创建了,你写的基本就没什么问题,顶多刷新或者重绘一下

我以前写的  
  给你个思路  
  -----------------------------------------  
  procedure   ChComState(b:   boolean;   pnl:   TPanel;   index:   Integer   =   0);   overload;   //设置Panel子组件的ReadOnly  
  var  
      i:   Integer;  
      ctr:   TControl;  
  begin  
      for   i   :=   0   to   pnl.ControlCount   -   1   do   begin  
          ctr   :=   pnl.Controls[i];  
          if   ctr.ClassType   =   TPanel   then  
              ChComState(b,   TPanel(ctr))   //递归  
          else   if   ctr.ClassType   =   TcxTextEdit   then   begin  
              TcxTextEdit(ctr).Properties.ReadOnly   :=   not   b;  
              if   index   =   1   then   begin  
                  TcxTextEdit(ctr).Style.Color   :=   clWindow;  
                  TcxTextEdit(ctr).StyleHot.Color   :=   clWindow;  
                  TcxTextEdit(ctr).StyleFocused.Color   :=   clWindow;  
              end  
              else   if   index   =   2   then   begin  
                  TcxTextEdit(ctr).Style.Color   :=   clInfoBk;  
                  TcxTextEdit(ctr).StyleHot.Color   :=   clInfoBk;  
                  TcxTextEdit(ctr).StyleFocused.Color   :=   clInfoBk;  
              end;  
          end  
          else   if   ctr.ClassType   =   TcxDateEdit   then  
              TcxDateEdit(ctr).Properties.ReadOnly   :=   not   b  
          else   if   ctr.ClassType   =   TcxTimeEdit   then  
              TcxTimeEdit(ctr).Properties.ReadOnly   :=   not   b  
          else   if   ctr.ClassType   =   TdxTimeEdit   then  
              TdxTimeEdit(ctr).ReadOnly   :=   not   b  
          else   if   ctr.ClassType   =   TcxLookupComboBox   then  
              TcxLookupComboBox(ctr).Properties.ReadOnly   :=   not   b  
          else   if   ctr.ClassType   =   tradiobutton   then  
              tradiobutton(ctr).Enabled   :=   b  
      end;  
  end;  
  ---------------------------------------------------------  
  procedure   ChComState(ctr:   TcxPageControl);   overload;   //动态菜单  
  {$IFDEF   DynamicMenus}  
  var  
      i:   Integer;  
      {$ENDIF}  
  begin  
      {$IFDEF   DynamicMenus}  
      for   i   :=   0   to   TcxPageControl(ctr).PageCount   -   1   do  
          TcxPageControl(ctr).Pages[i].TabVisible   :=   DM.Dst_Menu.locate('winformname',   TcxPageControl(ctr).Pages[i].Name,   [])  
              {$ENDIF}  
  end;  
  ---------------------------------------------------------  
  procedure   TSysAdmin.DsStateChange(Sender:   TObject);  
  var  
      ipos:   Integer;  
      dsName:   string;  
  begin  
      //****************共性****************  
      dsName   :=   TDataSource(Sender).Name;  
      ipos   :=   Pos('_',   dsName);  
      dsName   :=   Copy(dsName,   ipos   +   1,   Length(dsName)   -   ipos);  
      //功能按钮  
      chDsState(FindComponent('Btn_Add_'   +   dsName),  
          FindComponent('Btn_Edt_'   +   dsName),  
          FindComponent('Btn_Del_'   +   dsName),  
          FindComponent('Btn_Yes_'   +   dsName),  
          FindComponent('Btn_No_'   +   dsName),   TDataSource(Sender));  
      //编辑框  
      ChDescr(TDataSource(Sender).State   in   [dsInsert,   dsEdit],   TDataSource(Sender).Tag);  
  end;  
 

procedure   ChComState(frm:   TFrame);   overload;   //动态菜单  
  {$IFDEF   DynamicMenus}  
  var  
      i,   j:   Integer;  
      ctr:   TComponent;  
      {$ENDIF}  
  begin  
      {$IFDEF   DynamicMenus}  
      for   i   :=   0   to   frm.ComponentCount   -   1   do   begin  
          ctr   :=   frm.Components[i];  
          if   (ctr.ClassType   =   TcxPageControl)   and   (Pos(Pgc_WinFormName,   TcxPageControl(ctr).Name)   >   0)   then   begin  
              for   j   :=   0   to   TcxPageControl(ctr).PageCount   -   1   do  
                  TcxPageControl(ctr).Pages[j].TabVisible   :=   DM.Dst_Menu.locate('winformname',   TcxPageControl(ctr).Pages[j].Name,   []);  
              Break;  
          end;  
      end;  
      {$ENDIF}  
  end;

注意FindControl与FindComponent的区别  
   
  假如form1上有panel  
  panel1上拖放有控件label1,label2...  
  即label1,label2...的parent都为panel1  
   
  历遍时  
  (for   i   :=   0   to   form1.ComponentCount   -   1   do)  
  form1.Components为  
      panel1,  
      label1,label2...  
   
  (for   i   :=   0   to   panel1.ControlCount   -   1   do)  
  panel1.Controls为  
      label1,label2...  
   
  通过  
  panel1.FindControl  
  (可以找  
      label1,label2...)  
   
  或form1.FindComponent  
  (可以找  
      panel1,  
      label1,label2...)  
   
  找出控件后,判断条件处理

 
  TEdit(form2.FindComponent('Edit'+inttostr(i))).text   :=.....;  
  或  
  (form2.FindComponent('Edit'+inttostr(i))   as   TEdit).text   :=.....;

posted on 2008-09-19 13:44 delphi2007 阅读(2245) 评论(0)  编辑 收藏 引用
只有注册用户登录后才能发表评论。