vb.net 在 TabControl 中以编程方式创建选项卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15528120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically create tabs in TabControl
提问by nherrmann
I have a form containing a tabcontrol that I want to modify based on user input on a previous form. I have created a tabcontrol named "TabControl" (creative, I know), and am attempting to add a tab for each value of the array "tabNames()". When I debug the program, I enter the values into the array on the first form, and when the second form loads, I get nothing in my tabcontrol. Any thoughts?
我有一个包含 tabcontrol 的表单,我想根据用户在以前的表单上的输入进行修改。我创建了一个名为“TabControl”的 tabcontrol(创意,我知道),并试图为数组“tabNames()”的每个值添加一个选项卡。当我调试程序时,我将值输入到第一个表单的数组中,当第二个表单加载时,我的 tabcontrol 中什么也没有。有什么想法吗?
Public Sub frmContent_Load(ByVal sender As Object, ByVal e As EventArgs)
lblTitle.Text = frmiFormCreator.txtTitle.Text
For i As Integer = 0 To frmiFormCreator.numberOfTabs
Dim tabPage(frmiFormCreator.numberOfTabs) As TabPage
tabPage(i).Text = frmiFormCreator.tabNames(i)
TabControl.TabPages.Add(tabPage(i))
Next
End Sub
回答by LarsTech
Try creating the TabPage object:
尝试创建 TabPage 对象:
For i As Integer = 0 To frmiFormCreator.numberOfTabs
Dim newPage As New TabPage()
newPage.Text = frmiFormCreator.tabNames(i)
TabControl.TabPages.Add(newPage)
Next
Also, your code looks like it might be creating an extra tab. Maybe you want this:
此外,您的代码看起来可能正在创建一个额外的选项卡。也许你想要这个:
For i As Integer = 0 To frmiFormCreator.numberOfTabs - 1
回答by Nianios
Try
尝试
Public Sub frmContent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
lblTitle.Text = frmiFormCreator.txtTitle.Text
For i As Integer = 0 To frmiFormCreator.numberOfTabs
Dim tabPage As New TabPage(frmiFormCreator.tabNames(i))
TabControl.TabPages.Add(tabPage)
Next
End Sub

