【发布时间】:2018-05-24 05:45:51
【问题描述】:
我想将用户控件加载到带有按钮的面板中,但是当我尝试此代码时,它什么也没显示。我想知道我的问题是否有解决方案,因为我已经尝试了许多来自互联网的解决方案,但没有任何效果。
这是我的代码:
关于用户控制
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();
}
主窗体
用户控制
感谢您的关心。
【问题讨论】:
-
你希望用户控件是单例的?
-
对不起,我不明白
-
我想通过单击仪表板按钮向表单显示用户控件
-
兄弟,您的代码运行良好。确保您的面板正确放置并在表单上可见
-
将 UserControl 的静态实例添加到表单不是一个好主意。
标签: c# winforms user-controls panel