Jose Maria
Jose Maria

Reputation: 5

Visual Tabpages

Hellos guys, I'm trying to create tabpages dinamically everytime I clic a button so the problem is when I limit the creation of a tab that exist already, I tried Controls.Find method and got "Value of type 'Control()' cannot be converted to 'Boolean'"... Here is my code in Visual Studio 2015. I will appreciate any help or another method.

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim myTabPage As New TabPage()
    TabControl1.Visible = True

    If myTabPage.Controls.Find("Pedido", True) Then

        TabControl1.SelectedTab = myTabPage

    Else

        Form1.TopLevel = False
        myTabPage.Text = "Pedido" 
        TabControl1.TabPages.Add(myTabPage)
        myTabPage.Controls.Add(Form1)
        TabControl1.SelectedTab = myTabPage
        Form1.Show()
    End If

End Sub

Upvotes: 0

Views: 26

Answers (1)

Brian M Stafford
Brian M Stafford

Reputation: 8868

Controls.Find returns an array of controls, not a boolean. So do the following:

If myTabPage.Controls.Find("Pedido", True).Length > 0 Then

EDIT:

With the assumption you are trying to either select or create a tab called "Pedido" (and stripping out some of the code for clarity), try the following:

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      TabControl1.Visible = True

      Dim c = TabControl1.Controls.Find("Pedido", True)

      If c.Length > 0 Then
         TabControl1.SelectedTab = CType(c(0), TabPage)
      Else
         Dim myTabPage As New TabPage()
         myTabPage.Text = "Pedido"
         myTabPage.Name = "Pedido"
         TabControl1.TabPages.Add(myTabPage)
         TabControl1.SelectedTab = myTabPage
      End If
   End Sub

Upvotes: 2

Related Questions