简体   繁体   English

如何动态将标签页添加到标签控件并将 label 添加到标签页

[英]How can I add tabpage to tabcontrol dynamically and add label to tabpage

I'm trying to add some Tabages to an existing TabControl named "EMER_AUTO_TabContorl" and controlled by emer_num_textbox.我正在尝试将一些 Tabages 添加到名为“EMER_AUTO_TabContorl”并由 emer_num_textbox 控制的现有 TabControl。

As the picture below,I should be get "A Group"/"B Group"/"C Group", but "C Group"/"B Group"/"C Group".如下图,我应该得到“A组”/“B组”/“C组”,但“C组”/“B组”/“C组”。

在此处输入图像描述

And this is my code.这是我的代码。

Dim group_name As String() = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"} 'my group name
Dim mytabPage As TabPage = New TabPage 
Dim myLabel As Label = New Label
Dim emer_groupNum As Integer 'my input number
Try
        emer_groupNum = Emer_Num_TextBox.Text 
        EMER_AUTO_TabControl.TabPages.Clear() 'Clear my tabControl
Catch           
End Try

For i = 1 To emer_groupNum

   With myLabel
       .Text = "123"
       .Location = New Point(6, pos1_Y)
   End With

   With mytabPage
      .Text = group_name(i - 1) & " Group"         
      .Controls.Add(myLabel)
   End With

   EMER_AUTO_TabControl.TabPages.Add(mytabPage)               
Next

Beside Label text "123" doesn't show on TabPages.除了 Label 文本“123”不会显示在 TabPages 上。

You are re-using the same tabpage and label each time.您每次都在重复使用相同的标签页和 label。 Move the 'New' inside the for loop, as below:在 for 循环中移动“新建”,如下所示:

Dim group_name As String() = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"} 'my group name
Dim mytabPage As TabPage 
Dim myLabel As Label 
Dim emer_groupNum As Integer 'my input number
Try
        emer_groupNum = Emer_Num_TextBox.Text 
        EMER_AUTO_TabControl.TabPages.Clear() 'Clear my tabControl
Catch           
End Try

For i = 1 To emer_groupNum
   myLabel = New Label
   With myLabel
       .Text = "123"
       .Location = New Point(6, pos1_Y)
   End With

   mytabPage = New TabPage
   With mytabPage
      .Text = group_name(i - 1) & " Group"         
      .Controls.Add(myLabel)
   End With

   EMER_AUTO_TabControl.TabPages.Add(mytabPage)               
Next

EDIT编辑

You may also like to change how you test for a correct integer input as with the Try/Catch currently the For loop is run regardless.您可能还想更改测试正确的 integer 输入的方式,就像当前运行 For 循环的 Try/Catch 一样。 Maybe try something like this:也许尝试这样的事情:

   If Integer.TryParse(Emer_Num_TextBox.Text, emer_groupNum) Then
        For i = 1 To emer_groupNum
            ' Loop code here
        Next
    Else
        ' Handle incorrect input
    End If

Turn on Option Strict.打开选项严格。

You should test with.TryParse if the text box has a string that can be converted to an Integer.如果文本框有一个可以转换为 Integer 的字符串,您应该使用.TryParse 进行测试。

Never write an empty Catch.永远不要写一个空的 Catch。 Your code will just swallow errors and you won't know what has gone wrong.你的代码只会吞下错误,你不会知道出了什么问题。 In this code there is nothing that should go wrong with proper checking of user input.在这段代码中,go 在正确检查用户输入方面没有任何问题。

I think your problem is declaring your label and tab page outside of the loop.我认为您的问题是在循环之外声明您的 label 和标签页。 This only creates a single label and tab page.这只会创建一个 label 和标签页。 On each iteration of the loop you change the properties of the single controls and readd the same this to the collection.在循环的每次迭代中,您更改单个控件的属性并将相同的 this 读取到集合中。 Declare your controls inside the loop and you will get a new instance of the controls.在循环中声明您的控件,您将获得控件的新实例。

Private Sub OPCode()
    Dim group_name As String() = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"} 'my group name
    Dim emer_groupNum As Integer  'my input number
    If Not Integer.TryParse(TextBox1.Text, emer_groupNum) Then
        MessageBox.Show("Please enter a valid number in the box.")
        Exit Sub
    End If
    Dim pos1_Y As Integer = 3
    TabControl1.TabPages.Clear() 'Clear my tabControl
    For i = 1 To emer_groupNum
        Dim mytabPage As TabPage = New TabPage
        Dim myLabel As Label = New Label
        With myLabel
            .Text = "123"
            .Location = New Point(6, pos1_Y)
        End With
        With mytabPage
            .Text = group_name(i - 1) & " Group"
            .Controls.Add(myLabel)
        End With
        TabControl1.TabPages.Add(mytabPage)
    Next
End Sub

You could build all the TabPages at Form Load and store them in a List, then simply repopulate the TabControl from that list:您可以在表单加载时构建所有 TabPages 并将它们存储在一个列表中,然后只需从该列表中重新填充 TabControl:

Private Groups As New List(Of TabPage)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim group_names As String = "ABCDEFGHIJ"
    For i As Integer = 1 To group_names.Length
        Dim mytabPage As New TabPage
        mytabPage.Text = group_names(i - 1) & " Group"
        Groups.Add(mytabPage)

        Dim myLabel As New Label
        With myLabel
            .Text = "123"
            .Location = New Point(6, pos1_Y) ' <-- Not sure if "pos1_Y" changes?
        End With
        mytabPage.Controls.Add(myLabel)
    Next
End Sub

Private Sub Emer_Num_TextBox_TextChanged(sender As Object, e As EventArgs) Handles Emer_Num_TextBox.TextChanged
    Dim i As Integer
    If Integer.TryParse(Emer_Num_TextBox.Text, i) Then
        If i >= 0 AndAlso i <= Groups.Count Then
            EMER_AUTO_TabControl.TabPages.Clear()
            For Each tab As TabPage In Groups.Take(i)
                EMER_AUTO_TabControl.TabPages.Add(tab)
            Next
        End If
    End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM