如何在运行时以编程方式向表单添加选项卡? [英] How can i programmatically add a Tab to a form during runtime?
问题描述
我在谷歌上搜索了这个,但仍然无法让它工作.我知道如何使用工具箱添加选项卡.我还阅读了有关如何以编程方式执行此操作的信息,但我仍然不明白.(MSVC# Express 2010)
I googled this and still cant get it to work. I know how to add a tab using the toolbox. I have also read about how to do it programmatically, but i still dont get it. (MSVC# Express 2010)
我有一个简单的项目设置.只是一个带有 TabControl 的 Windows 窗体,我使用设计器添加了一个新的 TabControl 并将该 TabControl 设为公开而不是私有.
I have an easy project set up. Just a windows Form with a TabControl in it, i used the Designer to add a new TabControl and made that TabControl public instead of private.
我写了这段代码a) 访问 Windows 窗体b) 添加标签页.
I wrote this code to a) access the Windows Form b) add a tabpage.
代码编译正常,但运行时不显示Tabpage.
The code compiles just fine, but the Tabpage is not displayed during runtime.
static class Program
{
[STAThread]
static void Main()
{
Application.SetCompatibleTextRenderingDefault(false);
Application.EnableVisualStyles();
Form1 ApplicationMainForm = new Form1();
Application.Run(ApplicationMainForm); //LABEL B
ApplicationMainForm.tabControl1.TabPages.Add("MyPage"); //LABEL A
}
}
如何让表单显示我的标签页?当 A 行和 B 行改变位置时显示我的 TabPage.我是否缺少更新方法,或者在应用程序关闭之前从未调用过 TabPage Add?
How can i get the form to display my Tabpage? My TabPage is displayed when the Lines A and B change position. Am i missing an update method, oder is the TabPage Add never called until the application closes?
编辑 #1:一些小的编辑.编辑 #2:在更多示例代码中编辑.编辑 #3:删除了一些较早的/不相关的点.编辑 #4:找到一个提示并在
Edit #1: Some minor edits. Edit #2: Edited in some more examplecode. Edit #3: Removed some earlier / irelevant points. Edit #4: Found a hint and edited this information in
推荐答案
Form1.tabControl1.Controls.Add(myNewTabItem);
选项卡控件是选项卡页的集合,因此您可以像向集合中添加任何控件一样添加选项卡页.请注意,选项卡按您添加的顺序显示.
The tab control is a collection of tab pages, so you add tab pages like you add any control to a collection. Note that the tabs show up in the order you add them.
这篇关于如何在运行时以编程方式向表单添加选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!