VB.NET 添加带索引标签的 TabPage
VB.NET Add TabPage with Indexed Label
你想要完成什么?
我想将 TabPage 添加到预先存在的 TabControl,其中的 Label 具有索引名称。
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
你期望结果是什么?
每次单击该按钮时,都会添加一个新标签,标签名为 "Label1"、"Label2" 等
你得到的实际结果是什么?
此代码创建了一个新的标签页并添加了标签,但它始终命名为 Label1 并且没有增加 1。
为什么你有一个 For
循环?您只想添加一个 Label
,对吗?您应该创建一个 TabPage
,创建一个 Label
,将 Label
添加到 TabPage
,然后将 TabPage
添加到 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)
你想要完成什么?
我想将 TabPage 添加到预先存在的 TabControl,其中的 Label 具有索引名称。
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
你期望结果是什么?
每次单击该按钮时,都会添加一个新标签,标签名为 "Label1"、"Label2" 等
你得到的实际结果是什么?
此代码创建了一个新的标签页并添加了标签,但它始终命名为 Label1 并且没有增加 1。
为什么你有一个 For
循环?您只想添加一个 Label
,对吗?您应该创建一个 TabPage
,创建一个 Label
,将 Label
添加到 TabPage
,然后将 TabPage
添加到 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)