2016-06-12 112 views
2

当我写代码来添加用户控件的形式:表格查找用户控件添加

 UserControl edt = new UserControl(); 
     edt.Name = "ItemEdit"; 
     frm_Editor frm = new frm_Editor(); 
     frm.Controls.Add(edt); 
     frm.Show(); 

然后,我发现用户控件的形式:

Control[] tbxs = this.Controls.Find("ItemEdit", true); 
if (tbxs != null && tbxs.Length > 0) 
{ 
    MessageBox.Show("Found"); 
} 

但结果是空& & tbxs。长度= 0 请指导我解决过程中的问题。非常感谢!

+0

是'wpf'还是'winform'? – Shaharyar

+0

每次有人试图在没有MVVM的情况下使用WPF时,上帝会让一只小猫浪费两天的时间完成一项十分钟的小任务。 –

回答

0

它在这个小程序中起作用,它可能会指向您使用自己的代码的解决方案。

using System; 
using System.Windows.Forms; 

namespace FormTest 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      Form1 frm = new Form1(); 
      UserControl edt = new UserControl(); 
      edt.Name = "ItemEdit"; 
      frm.Controls.Add(edt); 

      Application.Run(frm); 
     } 
    } 
} 

using System.Windows.Forms; 

namespace FormTest 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, System.EventArgs e) 
     { 
      Control[] tbxs = this.Controls.Find("ItemEdit", true); 
      if (tbxs != null && tbxs.Length > 0) 
      { 
       MessageBox.Show("Found"); 
      } 
     } 
    } 
}