Link to home
Start Free TrialLog in
Avatar of JPERKS1985
JPERKS1985

asked on

TabControl1.TabPages.Contains Problem in VB.NET

Hey everyone I have a question that really bugging me. I read an example on msdn about how to check to see if a specified tab has already been created so I code the following

     'Bring up a new tab that lets you set the properties for that event
        Dim myTabPage As New TabPage
        myTabPage.Text = gridActiveEvent.Item(gridActiveEvent.CurrentCell.RowNumber, 2) & ":" & gridActiveEvent.Item(gridActiveEvent.CurrentCell.RowNumber, 0)

Debug.Write(myTabPage)
        'Make sure tab wasn't already created
       If TabControl1.TabPages.Contains(myTabPage) = False Then

            TabControl1.TabPages.Add(myTabPage)
            TabControl1.SelectedTab = (myTabPage)
        Else
            Exit Sub

        End If
     


The code doesn't work and a new tab is created even though the tab already exists. Debug.wrote wrote this

TabPage: {09003C6DD447D81C:0}

Both times i ran this code, the the tabs are the same.


Thanks in advance for the help guys.
ASKER CERTIFIED SOLUTION
Avatar of vadim63
vadim63
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It should be like this:

Dim myTabPage As New TabPage
Dim Check as Boolean
Dim PageText as String = gridActiveEvent.Item(gridActiveEvent.CurrentCell.RowNumber, 2) & ":" & gridActiveEvent.Item(gridActiveEvent.CurrentCell.RowNumber, 0)
        myTabPage.Text = PageText

      For i As Integer = 0 To Me.TabControl1.TabCount - 1
            If Me.TabControl1.TabPages(i).Text = PageText Then
                Check = True
                Exit For
            Else
                Check = False
            End If
        Next

Debug.Write(myTabPage)
        'Make sure tab wasn't already created
       If Check = False Then

            TabControl1.TabPages.Add(myTabPage)
            TabControl1.SelectedTab = (myTabPage)
        Else
            Exit Sub

        End If
After "Check = True", should be: "Exit For"
Or like this:

Private Function CheckIt(ByVal pText As String) As Boolean
        For i As Integer = 0 To Me.TabControl1.TabCount - 1
            If Me.TabControl1.TabPages(i).Text = pText Then
                CheckIt = True
                Exit For
            Else
                CheckIt = False
            End If
        Next
    End Function

Dim myTabPage As New TabPage
Dim PageText as String = gridActiveEvent.Item(gridActiveEvent.CurrentCell.RowNumber, 2) & ":" & gridActiveEvent.Item(gridActiveEvent.CurrentCell.RowNumber, 0)
        myTabPage.Text = PageText

Debug.Write(myTabPage)
        'Make sure tab wasn't already created
       If CheckIt(myTabPage.Text) = False Then
            TabControl1.TabPages.Add(myTabPage)
            TabControl1.SelectedTab = (myTabPage)
        End If