繁体   English   中英

添加后在窗体中查找UserControl

[英]Find UserControl in Form when Added

我编写代码以在窗体中添加UserControl:

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

然后,我在窗体中找到UserControl:

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

但是结果为null && tbxs.Length = 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");
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM