How to add controls into a new tab page which is dynamically created?

I am developing a windows form application in C# which has a tab control with dynamically created tab pages. I want to add same layout and controls (ComboBoxes, TextBoxes, Buttons & DataGridView) from ComboSet Item 1 to a newly created tab page (ComboSet Item 2 in this case). How can I do that and how to name the controls?

New tabs will be generated from 1,2,3... n. So 'n' number of DataGridViews will be also added under the new tabs. Is there a way to bind these DataGridViews and is it possible to do so?

Any help will be very much appreciated!

Answers


Create a new user control->Place all controls into user control->create the properties for all controls. then you just have to manage only your user control.

void AddTab()
{
    TabPage tbp = new TabPage();
    tbp.Name=TabControl1.TabPages.Count.ToString();
    MyUserControl cnt = new MyUserControl();
    cnt.Name="Cnt" + tbp.Name;
    cnt.Dock=DockStyle.Fill;
    tbp.Controls.Add(cnt);
}

If you cannot place your code into the user control then you can create events for each control. for example ProductName_SelectedValueChange for ProductName combobox, Validating for Discount value and handle it into AddTab() method.


TabPages are normal container like Panel. You can use the tabpage.controls.add() method to add some new items. If you want to name a item use the myitem.Name = "Test" Property. If you want a fix set of items on more pages you should create a Usercontrol for it.


As you want to add more controls to the tab page, I suggest to create an own UserControl for the content in the tabe page.

Arrange the controls in the UserControl as you need it (DockStyle, Anchor...).

When you create a new tab just create a new UserControl and add it to the tab page as mentioned by Sebi. Additionally you should set the DockStyle to Fill. That should so the trick.


Need Your Help

XML Serialization assembly for non-web projects

c# vb.net serialization msbuild sgen

I am attempting to solve the well known problem of automatically generated serialization assemblies in VS 2010, both VB.NET and C#. The "Generate Serialization Assemblies" option in project setting...

Interprocess communication on the same box - communication between 2 applications or processes

c# .net ipc

What is the best way to implement interprocess communication between applications that are on the same box -- both written in c#?