从附加到tabcontrol的UserControl将新选项卡添加到tabcontrol中 [英] Add new tab into tabcontrol from an UserControl that attached to tabcontrol
本文介绍了从附加到tabcontrol的UserControl将新选项卡添加到tabcontrol中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想添加一个新选项卡,并将一个控件从附加到tabcontrol的UserControl连接到我的tabcontrol。
我使用了DotNetBar组件。
我在form1中使用下面的代码,我想从UserControl调用它
public void NewTab( string TabName)
{
TabItem newtab1;
usercontrol_1 a1 = new usercontrol_1();
a1.Dock = DockStyle.Fill;
newtab1 = this .tabControl1.CreateTab(TabName);
newtab1.AttachedControl.Controls.Add(a1);
this .tabControl1.SelectedTab = newtab1;
}
这是我从UserControl调用此函数的方式:
Form1 f1 = new Form1();
f1.NewTab( new tab);
f1.show();
但我不想打开另一个Form1!
解决方案
尝试类似
foreach (表单形式 in Application.OpenForms)
{
if (form Form1)
{
Form1 f =(Form1)表格;
f.NewTab( new tab);
break ;
}
}
更改以下代码
Form1 f1 =( this .Parent as Form1);
f1.NewTab( new tab);
I want to add a new tab and attach a control into my tabcontrol from an UserControl that attached to the tabcontrol.
I used DotNetBar Components.
I use the code below in form1 and I want to call It from UserControl
public void NewTab (string TabName)
{
TabItem newtab1;
usercontrol_1 a1 = new usercontrol_1();
a1.Dock = DockStyle.Fill;
newtab1 = this.tabControl1.CreateTab(TabName);
newtab1.AttachedControl.Controls.Add(a1);
this.tabControl1.SelectedTab = newtab1;
}
this is how I call this function from UserControl:
Form1 f1 = new Form1();
f1.NewTab("new tab");
f1.show();
But I don't want to open another Form1!
解决方案
Try something like
foreach (Form form in Application.OpenForms) { if (form is Form1) { Form1 f = (Form1)form; f.NewTab("new tab"); break; } }
change the code as below
Form1 f1 = (this.Parent as Form1); f1.NewTab("new tab");
这篇关于从附加到tabcontrol的UserControl将新选项卡添加到tabcontrol中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文