将UserControl2添加到MainForm,从程序上添加UserControl1到MainForm

时间:2018-04-07 16:50:36

标签: c#

我有一个有面板的MainForm。我在启动应用程序时添加了一个UserControl1。通过单击UserControl1内部的按钮尝试调用UserControl2以添加面板并从主窗体面板清除UserControl1。然后单击UserControl2内部的第二个按钮,尝试调用UserControl1并从主窗体面板中清除UserControl2。但是在我的代码中,当我调用UserControl2时,MainForm变为空白。问题是什么?这是代码:

namespace UserControlTest
{
    public partial class MainForm : Form // main from
    {
        private static MainForm _instance;
        public static MainForm Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new MainForm();
                }
                return _instance;
            }
        }

        public MainForm()
        {
            InitializeComponent();

            if (!panel1.Controls.Contains(UserControl1.Instance)) // checking UserControl1 existance
            {
                panel1.Controls.Add(UserControl1.Instance); // add
                UserControl1.Instance.Dock = DockStyle.Top; // dock it 
                UserControl1.Instance.BringToFront(); // bring to front of panel1
            }

        }

        public void CallUserControl1() // method helps to bring UserControl1 to front of panel 1
        {
            if (!panel1.Controls.Contains(UserControl1.Instance))
            {
                panel1.Controls.Clear();// clearing controls
                panel1.Controls.Add(UserControl1.Instance);
                UserControl1.Instance.Dock = DockStyle.Top;
                UserControl1.Instance.BringToFront();
            }
            else
            {
                UserControl1.Instance.BringToFront();
            }
        }
        public void CallUserControl2()// method helps to bring UserControl2 to front of panel 1
        {
            if (!panel1.Controls.Contains(UserControl2.Instance))
            {
                panel1.Controls.Clear();// clearing controls
                panel1.Controls.Add(UserControl2.Instance);
                UserControl2.Instance.Dock = DockStyle.Top;
                UserControl2.Instance.BringToFront();
            }
            else
            {
                UserControl2.Instance.BringToFront();
            }
        }
    }
}

//第一个用户控件,它有助于将UserControl2调用到首页

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 button1_Click(object sender, EventArgs e)
    {
        MainForm mainForm = new MainForm();
        mainForm.CallUserControl2();
    }
}

//第二个用户控件,它有助于调用UserControl1来到首页

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();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MainForm main = new MainForm();
        main.CallUserControl1();
    }
}

更新:感谢@RonBeyer的评论,这是有效的,但需要首先清除面板控件

var main = this.FindForm() as MainForm;
main.CallUserControl1();

0 个答案:

没有答案