簡體   English   中英

如何通過在C#中單擊按鈕將用戶控件加載到面板

[英]How to load user control to a panel by button click in C#

我想用一個按鈕將用戶控件加載到面板,但是當我嘗試這個代碼時,它什么都沒顯示。 我想知道我的問題是否有解決方案,因為我已經嘗試過很多來自互聯網的解決方案,但沒有任何效果。

這是我的代碼:

在用戶cotrol上

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

按下按鈕點擊winform

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

主要形式

在此輸入圖像描述

用戶控制

在此輸入圖像描述

感謝你的關心。

嘗試這個。 希望這個能對您有所幫助

private void set2ControlTopanel(control f) {

    try {
        p2Form = f;
        p2Form.Dock = DockStyle.Fill;
        p2Form.Show();
        panelTop.Controls.Add(p2Form);
        p2Form.BringToFront();
    }
    catch (Exception ex) {
        MsgBox(ex.Message);
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM