What are you trying to accomplish?
I want to add a TabPage to a pre-existing TabControl with a Label that has an indexed name.
Private Sub BtnAddReport_Click(sender As Object, e As EventArgs) Handles BtnAddReport.Click
Dim rep As New OpenFileDialog
rep.Title = "Add Report"
rep.InitialDirectory = "C:\Customers"
rep.FileName = ""
rep.DefaultExt = ".html"
rep.Filter = "HTML Documents|*.html"
rep.Multiselect = False
If rep.ShowDialog() = DialogResult.OK Then
Dim newTab As TabPage = New TabPage With {.Text = Path.GetFileName(rep.FileName)}
TabControl2.Controls.Add(newTab)
Dim i As Integer
For i = 1 To TabControl2.TabPages.Count
Dim lbl As Label = New Label With {.Text = "Label" & i, .Location = New Point(3, 3)}
newTab.Controls.Add(lbl)
Next
End If
End Sub
What do you expect the result to be?
Each time the button is clicked, a new tab is added with a label named "Label1", "Label2", etc.
What is the actual result you get?
This code creates a new tabpage and adds the label, but it's always named Label1 and does not increase by 1.
Why do you have a For
loop in there? You only want to add one Label
, right? You should be creating one TabPage
, creating one Label
, adding the Label
to the TabPage
, then adding the TabPage
to the TabControl
.
Dim newTab As TabPage = New TabPage With {.Text = Path.GetFileName(rep.FileName)}
Dim lbl As Label = New Label With {.Text = "Label" & (TabControl2.TabPages.Count + 1), .Location = New Point(3, 3)}
newTab.Controls.Add(lbl)
TabControl2.TabPages.Add(newTab)