【问题标题】:Dynamically loading UserControl after clicking button on another UserControl单击另一个 UserControl 上的按钮后动态加载 UserControl
【发布时间】:2018-07-02 20:19:28
【问题描述】:

我在表单上显示 UserControl 时遇到问题。

简而言之程序:

  1. 在 Form1 中有一个按钮。单击此按钮后,在面板(容器)中,我的第一个 UserControl(new.cs)被动态加载。
  2. 在那个面板上,我有另一个按钮,它指向另一个 UserControl (choice.cs),我想在 Form1 上的同一个面板(容器)中显示它。

第一点效果很好,但我对第二点有疑问。我想我必须更正choice_button_Click 功能。有什么简单的方法吗?

这是我的代码:

Form1.cs:

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

        private void new_button_Click(object sender, EventArgs e)
        {
            if (!container.Controls.Contains(@new.Instance))
            {
                container.Controls.Add(@new.Instance);
                @new.Instance.Dock = DockStyle.Fill;
                @new.Instance.BringToFront();
            }
            else
            {
                @new.Instance.BringToFront();
            }
        }

        public Panel getContainer()
        {
            return container;
        }
    }
}

new.cs:

namespace WindowsFormsApp1
{
    public partial class @new : UserControl
    {
        private static @new _instance;
        public static @new Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new @new();
                return _instance;
            }
        }

        public @new()
        {
            InitializeComponent();
        }

        private void choice_button_Click(object sender, EventArgs e)
        {
            using (Form1 main = new Form1())
            {
                if (!main.getContainer().Controls.Contains(choice.Instance))
                {
                    main.getContainer().Controls.Add(choice.Instance);
                    choice.Instance.Dock = DockStyle.Fill;
                    choice.Instance.BringToFront();
                }
                else
                {
                    choice.Instance.BringToFront();
                }
            }    
        }
    }
}

choice.cs:

namespace WindowsFormsApp1
{
    public partial class choice : UserControl
    {
        private static choice _instance;
        public static choice Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new choice();
                return _instance;
            }
        }

        public choice()
        {
            InitializeComponent();
        }
    }
}

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    您的功能问题是您没有将choice.Instance 放在您的Form1 实例中。您正在创建一个新表单,将其放置在那里,然后丢弃该表单。

    但是,您的设计中也存在问题,您违反了 UserControl 不应直接访问和修改其父表单的原则。您最好向@new 添加一个事件,通过单击按钮引发该事件,然后在您的表单实例中处理该事件。

    例如:

    new.cs:

    namespace WindowsFormsApp1
    {
        public partial class @new : UserControl
        {
            private static @new _instance;
            public static @new Instance
            {
                get
                {
                    if (_instance == null)
                        _instance = new @new();
                    return _instance;
                }
            }
    
            public event EventHandler StepCompleted;
    
            public @new()
            {
                InitializeComponent();
            }
    
            private void choice_button_Click(object sender, EventArgs e)
            {
                StepCompleted?.Invoke(this, EventArgs.Empty);
            }
        }
    }
    

    和Form1.cs:

    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void new_button_Click(object sender, EventArgs e)
            {
                if (!container.Controls.Contains(@new.Instance))
                {
                    container.Controls.Add(@new.Instance);
                    @new.Instance.Dock = DockStyle.Fill;
                    @new.Instance.BringToFront();
                }
                else
                {
                    @new.Instance.BringToFront();
                }
            }
    
            private void new_StepCompleted(object sender, EventArgs e)
            {
                if (!container.Controls.Contains(choice.Instance))
                {
                    container.Controls.Add(choice.Instance);
                    choice.Instance.Dock = DockStyle.Fill;
                    choice.Instance.BringToFront();
                }
                else
                {
                    choice.Instance.BringToFront();
                }
            }
        }
    }
    

    现在您显然要处理同一个表单实例,因为它本身就是处理事件的实例。此外,@new 无需进行任何尴尬的查找即可找到正确的 Form1 实例并修改表单。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多