Mario.C
Mario.C

Reputation: 65

C# usercontrol show

I have created one user control with some things on it, and I need to know if it's possible in my form1 click in one button and that button open my usercontrol but not inside the form1.
I want to see the usercontrol separated from the form1, so if the user want to close the usercontrol he will close it and can keep the from1, or if the user want's to minimize the form1 and keep the usercontrol in the screen.

i have tested with this

                        UC lauchUC = new UC(person);
                        lauchUC.Show();

but that don't show nothing, and also tested with this:

                        UC lauchUC = new UC(person);
                        this.Controls.Add(lauchUC);

but it appears in the form

can someone help me or telling me if it's possible show it separated from the form?

Upvotes: 2

Views: 7965

Answers (2)

Jeroen van Langen
Jeroen van Langen

Reputation: 22073

You could pass an instance of your UserControl to the constructor of the Form. In this constructor, you can add it to it's Controls. Just create a new Form and alter it's constructor.

The (container) Form:

public partial class Form1 : Form
{
    public Form1(UserControl control)
    {
        InitializeComponent();
        this.Controls.Add(control);
    }
}

How to open it.

public void ButtonClick(object sender, EventArgs e)
{
    var myControl = new MyUserControl();
    var form = new Form1(myControl);
    form.Show();
}

Upvotes: 4

Beldi Anouar
Beldi Anouar

Reputation: 2180

You can Place it in a Window and call Window.ShowDialog.

 private void Button1_Click(object sender, EventArgs e)
    {
        Window window = new Window 
        {
            Title = "My User Control Dialog",
            Content = new UC(person)
        };

        window.ShowDialog();
    }

Upvotes: 1

Related Questions