LabRat
LabRat

Reputation: 2014

danamically creating controlltabs that can be used

I'm trying to dynamically create tabcontrols which works fine; the tabs are being created however. Once created I would also like them to become clickable and execute other code, this now posing a problem.

The code I’m using to create a tab is as follows

            ' do whatever wtih filename
        Dim myTabPage As New TabPage()
        myTabPage.Text = TextBox4.Text
        TabControl1.TabPages.Add(myTabPage)

        TabPage1.Hide()

Not so nice cause I can now fill my form with as many tabs as I like however none of them can be clicked to execute futher code???

EDIT:

    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

Upvotes: 0

Views: 49

Answers (1)

LarsTech
LarsTech

Reputation: 81610

You have to add an event handler for the TabPage click event:

Dim myTabPage As New TabPage
myTabPage.Text = TextBox4.Text
AddHandler myTabPage.Click, AddressOf TabPage1_Click
TabControl1.TabPages.Add(myTabPage)

Which will call this code:

Private Sub TabPage1_Click(sender As Object, e As EventArgs)
  MessageBox.Show(DirectCast(sender, TabPage).Text)
End Sub

Per your edit, you would have to add the name property:

myTabPage.Name = TextBox4.Text

And your SelectedIndexChanged event:

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

Upvotes: 1

Related Questions