我正在尝试动态创建工作正常的tabcontrols;然而,正在创建选项卡。一旦创建,我也希望它们变得可点击并执行其他代码,这现在构成了一个问题。
我用来创建标签的代码如下
' do whatever wtih filename
Dim myTabPage As New TabPage()
myTabPage.Text = TextBox4.Text
TabControl1.TabPages.Add(myTabPage)
TabPage1.Hide()
不太好,因为我现在可以用我喜欢的多个标签填充我的表单,但是没有一个可以点击执行更多的代码???
编辑:
Private Sub TabControl_SelectedIndexchaged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
Dim TabName As String
TabName = TabControl1.SelectedTab.Name
If TabName.Contains("TabPage") Then
' Do something
MsgBox("new tab created")
End If
End Sub
答案 0 :(得分:1)
您必须为TabPage单击事件添加事件处理程序:
Dim myTabPage As New TabPage
myTabPage.Text = TextBox4.Text
AddHandler myTabPage.Click, AddressOf TabPage1_Click
TabControl1.TabPages.Add(myTabPage)
将调用此代码:
Private Sub TabPage1_Click(sender As Object, e As EventArgs)
MessageBox.Show(DirectCast(sender, TabPage).Text)
End Sub
根据您的编辑,您必须添加名称属性:
myTabPage.Name = TextBox4.Text
您的SelectedIndexChanged事件:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) _
Handles TabControl1.SelectedIndexChanged
If TabControl1.SelectedTab IsNot Nothing Then
MessageBox.Show(TabControl1.SelectedTab.Name)
End If
End Sub