How to clone container control (with child control inside it) like a group box

H

harishashim

I'll make this post as short as possible.

1. Put a GroupBox on a window form. Put a Button and TextBox inside the
GroupBox .
2. Select the group box, do copy and paste inside the same windows form
(or different window form, doesnt matter).
3. Now I have another copy (or clone) of the GroupBox with a Button
and a Texbox control inside it. They have different name (should be
groupBox2, button2 and textBox2).

However I want to do this with code. I try this

----------------------------------------------------------------------
GroupBox groupNewBox = groupBox1;
----------------------------------------------------------------------

But this is groupNewBox is referencing groupBox1.

I can do this,

----------------------------------------------------------------------
GroupBox groupNewBox = new GroupBox();
groupNewBox.Text = groupBox1.Text;
Foreach( Control ctrl in groupBox.Controls)
{
//take each control and manually recreate the
//control as child of groupNewBox
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top