0

i just created an application using C# in Visual Studio. I am wondering is it possible to create a new instance of group-box with text-box and label as shown in the picture when user click add a new tab button(+). For example when the user opens a google chrome, whenever the user opens the new tab, it will show the same search box on each page. As for now I am able to create new tab page when the user clicks the add button but the new tab page will empty.

enter image description here

piet.t
  • 11,718
  • 21
  • 43
  • 52
  • 1
    You can create a user control with a group box and textbox and label inside it. At runtime create an instance of user control and add it to the form. – Chetan Mar 12 '19 at 10:12
  • I think you'll have to be more specific and show what you've attempted to. Otherwise, as it stands the question may be flagged / closed by Stackoverflow users for being too broad (opinion-based) – onlyphantom Mar 12 '19 at 10:15
  • 2
    It's possible. How do you add a tab page now? What is this "+" tab? My guess it's another blank tab.. have you been [here](https://stackoverflow.com/q/6738126/1997232)? – Sinatr Mar 12 '19 at 10:23

1 Answers1

0

You need to use "using System.Reflection;"

And is basically this:

TabPage tpOld = tabControl1.SelectedTab;
TabPage tpNew = new TabPage();
foreach (Control c in tpOld.Controls)
{
    Control cNew = (Control)Activator.CreateInstance(c.GetType());
    PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(c);
    foreach (PropertyDescriptor entry in pdc)
    {
        object val = entry.GetValue(c);
        entry.SetValue(cNew, val);
    }
    tpNew.Controls.Add(cNew);
}
tabControl1.TabPages.Add(tpNew);

This code will copy all the controls of the old tabPage and put them into a new tabpage with the exact controls, positions, sizes of the old one.

Credits here: here.

GuidoG
  • 11,359
  • 6
  • 44
  • 79
Mikev
  • 2,012
  • 1
  • 15
  • 27
  • Thank you very much, now i am able to clone the control in tab page 1 into new tab pages.But its seems like new control in tab pages 2 is unable to be edit or write anything into and text box while it is able on tab page 1. – Salahuddin Mar 14 '19 at 07:38