Question

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?

enter image description here

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!

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top