Spoon Yukina
Spoon Yukina

Reputation: 533

How can I Display UserControl into a form

I have created a UserControl and I want when I click on some button to display this UserControl into my form.

Is there any way to do that ?

Upvotes: 3

Views: 32816

Answers (4)

HOKBONG
HOKBONG

Reputation: 795

Is it a typical ascx type of control? If yes then you can set the "visible" property of the control through the button click event.

Let say you have :

<uc1:ft ID="userctrl"  runat="server" Visible="false" />

then on your button event:

protected void Button1_Click(object sender, EventArgs e)
{
    userctrl.Visible = true;
}

Upvotes: 0

Adam Plocher
Adam Plocher

Reputation: 14243

You can dynamically add the user control to the forms Controls collection.

For example, in the button click event handler:

MyUserControl uc = new MyUserControl();
uc.Dock = DockStyle.Fill;
this.Controls.Add(uc);

Upvotes: 11

Hamed
Hamed

Reputation: 2168

Drag your user control to your form and change it's Visible property to false

UserControl1.Visible = false;

Then on your button set the Visibility of your usercontrol to true.

private void button1_Click(object sender, EventArgs e)
{
    UserControl1.Visible = true;
}

Upvotes: 0

Steve Wellens
Steve Wellens

Reputation: 20640

Put it on the form. Start it out invisible, when the button is clicked set it to visible.

Upvotes: 0

Related Questions