【问题标题】:c# \ not able to load user control from another user control buttonc#\无法从另一个用户控件按钮加载用户控件
【发布时间】:2019-03-15 10:58:18
【问题描述】:

我正在 c# windows 窗体应用程序上测试此代码。这是我正在做的测试。

Panel1 有一个“p1Button”。 “UserControl1”有一个“uc1Button” 单击“p1Button”以在 Panel1 中加载“UserControl1”
当我在“uc1Button”上单击事件时找不到 panel2

是否可行或有什么建议?

// Form1.cs

using System;
using System.Windows.Forms;

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

        private void p1Button_Click(object sender, EventArgs e)
        {
            if (!panel1.Controls.Contains(UserControl1.Instance))
            {
                panel1.Controls.Add(UserControl1.Instance);
                UserControl1.Instance.BringToFront();
            }
            else
            {
                UserControl1.Instance.BringToFront();
            }
        }
    }
}

// UserControl1.cs

using System;
using System.Windows.Forms;

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

        private void uc1Button_Click(object sender, EventArgs e)
        {

        }
    }
}

// UserControl2.cs

using System.Windows.Forms;

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

【问题讨论】:

  • 你的用户控件是单例的,为什么?您需要了解事件和委托以及事件处理程序...
  • 正如其他人已经提到的那样,将它们设为单例是没有用的,我建议阅读控件的生命周期,并稍微深入一点。 Initialize 方法是通过构造函数调用的所以现在如果你有单例,ctor 将被调用一次。因此不会发生布局。您应该问的下一个问题是布局代码在哪里,答案在类声明“部分类”中阅读所有这些

标签: c# windows-forms-designer


【解决方案1】:

首先,您不应该将控件设置为单例。 其次,您必须了解父子边界。

你的父母是 Form。表单有两个孩子 - 面板。面板 1 有另外两个孩子 - 您的用户控件和一个按钮。您的用户控件还有其他子控件 - 按钮。

因此,如果您想从 usercontrol1 将某些内容放在面板 2 上,您至少有两个选择:

  1. 糟糕的方式。 这是不好的方式,你不应该这样做,因为它严格地绑定和耦合你的用户控件和你的表单。 您应该从用户控件(即面板)中获取父级,然后从该面板(即表单)中获取父级,然后查看另一个面板。

  2. 好方法。 简单又好。将事件处理程序添加到您的用户控件,例如:

    公共用户控件1:用户控件 { 公共事件 EventHandler ButtonClicked; }

然后在你的按钮中点击用户控件:

ButtonClicked?.Invoke(this, EventArgs.Empty);

然后你需要将你的主表单注册到这个处理程序:

UserControl1.ButtonClicked += (s, e) =>
{
    //show usercontrol2 on your panel.
}

您应该阅读有关 C# 中的委托和事件的更多信息。

【讨论】:

    猜你喜欢
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    相关资源
    最近更新 更多