C# Winform Create Dynamic TabPage and add User Control
Tuesday, November 22, 2016 Category : C-sharp.NET 0
List<string> openTabs = new List<string>();
List<TabPage> openTabPages = new List<TabPage>();
private void OpenPage(Control control, string OperationName)
{
if (!openTabs.Exists(m => m.ToString() == OperationName))
{
TabPage tb = new TabPage();
tb.Text = OperationName;
tb.Controls.Add(control);
tabControl1.TabPages.Add(tb);
// set align position
int main_width = tabControl1.Width;
int control_width = control.Width;
int posX = (main_width - control_width) / 2;
control.Location = new Point(posX, 0);
tabControl1.SelectedTab = tb;
openTabs.Add(OperationName);
openTabPages.Add(tb);
}
else
{
foreach (TabPage tb in tabControl1.TabPages)
{
Control ctl = tb.Controls[0];
if (ctl != null)
{
if (ctl.GetType() == control.GetType())
{
tabControl1.SelectedTab = tb;
}
}
}
}
}