如何使用FindComponent來調用另一個form中名字有規律的幾個TEdit

來源:互聯網
上載者:User
如何使用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   :=.....;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.