'How to load user control to a panel by button click in C#
I wanted to load a user control to a panel with a button, but when I tried this code it shows nothing. I wonder if there is a solution for my problem because I've already tried many solutions from internet but nothing worked.
this is my code :
on user 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();
}
}
on winform by button click
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();
}
main form
user control
Thanks for your concern.
Solution 1:[1]
try this. Hope this will help .you
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);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tanmay Nehete |