单击按钮时,Windows窗体中的Instance UserControl来自另一个类库

时间:2017-12-31 11:17:01

标签: c# winforms user-controls

我有一个主项目,它有一个面板和菜单。当用户单击该按钮时,我想加载用户控件。

我为UI创建了类库。但是当我单击按钮时,它不会在主项目中加载用户控件。问题是什么?

主要项目:

private void BtnMainPage_Click(object sender, EventArgs e)
{
        if (!PanelHome.Controls.Contains(Home.Instance))
        {
            PanelHome.Controls.Add(Home.Instance);
            Home.Instance.Dock = DockStyle.Fill;
            Home.Instance.BringToFront();
        }
        else
        {
            Home.Instance.BringToFront();
        }
}

用户界面:

public partial class Home : UserControl
{
    #region Variable
    public static Home Instance
    {
        get
        {
            return Singleton<Home>.GetInstance();
        }
    }
    #endregion

    public Home()
    {
        InitializeComponent();
        MessageBox.Show("Load");
    }
}

Singleton:

public sealed class Singleton<T> where T : class, new()
{
    private volatile static T Instance;
    private static object _lock = new object();

    public static T GetInstance()
    {
        if (Instance == null)
        {
            lock (_lock)
            {
                if (Instance == null)
                {
                    Instance = new T();
                }
            }
        }
        return Instance;
    }
}

1 个答案:

答案 0 :(得分:-1)

private void FrmDashboard_Load(object sender, EventArgs e)
{
    DataTable dt = DAl.GetTables();
    DataTable dt1;

    if (dt.Rows.Count > 0)
    {

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            o = new Available();
            if (dt.Rows[i]["Status"].ToString() == "A         ")
            {`enter code here`
                o.lblTable.BackColor = Color.DarkSeaGreen;
            }
            if (dt.Rows[i]["Status"].ToString() == "B         ")
            {
                o.lblTable.BackColor = Color.DarkRed;
            }
            else if (dt.Rows[i]["Status"].ToString() == "C         ")
            {
                o.lblTable.BackColor = Color.Blue;
            }
            o.ButtonClick1 +=new Available.EventHandler(o_ButtonClick1);       
            // MessageBox.Show(o.vButton1.Tag.ToString());
            o.lblTable.Text = i + "";
             panel.Controls.Add(o);
        }
    }   
}

protected void o_ButtonClick1(object sender, EventArgs e)
{
   MessageBox.Show(o.lblTable.Text);
}