以编程方式在TabControl中创建选项卡

时间:2013-03-20 15:39:31

标签: vb.net winforms tabcontrol

我有一个包含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

2 个答案:

答案 0 :(得分:5)

尝试创建TabPage对象:

For i As Integer = 0 To frmiFormCreator.numberOfTabs
  Dim newPage As New TabPage()
  newPage.Text = frmiFormCreator.tabNames(i)
  TabControl.TabPages.Add(newPage)
Next

此外,您的代码看起来可能正在创建一个额外的标签。也许你想要这个:

For i As Integer = 0 To frmiFormCreator.numberOfTabs - 1

答案 1 :(得分:1)

尝试

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