tabcontrol - how to remove a tabpage by text ?

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 440 Views - Last Post: 20 March 2019 - 04:46 AM Rate Topic: -----

#1 ZeevGoldstein   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-March 19

tabcontrol - how to remove a tabpage by text ?

Posted 19 March 2019 - 02:16 AM

hello,

i need to remove a tabpage by its text not by its name
i know how to remove by name but by text i find it a bit tricky

by text i mean by its caption - the text you see on the tabpage header

a simple sample will be much appreciated

thanks
Is This A Good Question/Topic? 0
  • +

Replies To: tabcontrol - how to remove a tabpage by text ?

#2 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6777
  • View blog
  • Posts: 27,959
  • Joined: 12-December 12

Re: tabcontrol - how to remove a tabpage by text ?

Posted 19 March 2019 - 02:35 AM

Can you post the code that you would use to loop through the tabpages? That would be the first step.

And for what application type is this? WPF? WinForms? etc.
Was This Post Helpful? 0
  • +
  • -

#3 ZeevGoldstein   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-March 19

Re: tabcontrol - how to remove a tabpage by text ?

Posted 19 March 2019 - 02:38 AM

thanks for your reply

there is no loop

if a condition is true say on startup i just want to remove the tabpages
i don't need to restore them after that
just remove but by the tabpage text

hope i made myself clear...

once again - thanks
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6777
  • View blog
  • Posts: 27,959
  • Joined: 12-December 12

Re: tabcontrol - how to remove a tabpage by text ?

Posted 19 March 2019 - 02:50 AM

You have not confirmed what type of application this is.

Anyway, if WinForms, a TabPage has a Text property. Read this property and compare it.
Was This Post Helpful? 0
  • +
  • -

#5 ZeevGoldstein   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-March 19

Re: tabcontrol - how to remove a tabpage by text ?

Posted 19 March 2019 - 03:25 AM

vb.net winforms application

that's exactly what i'm asking -
how do i get this text and remove the tabpage by it ?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6777
  • View blog
  • Posts: 27,959
  • Joined: 12-December 12

Re: tabcontrol - how to remove a tabpage by text ?

Posted 19 March 2019 - 04:13 AM

I have described what you need to do. Give it a go and post your attempt. We are here to encourage not hand out completed code.
Was This Post Helpful? 0
  • +
  • -

#7 ZeevGoldstein   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-March 19

Re: tabcontrol - how to remove a tabpage by text ?

Posted 19 March 2019 - 04:26 AM

how do i read this property ?
if i knew that i would have the solution
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6777
  • View blog
  • Posts: 27,959
  • Joined: 12-December 12

Re: tabcontrol - how to remove a tabpage by text ?

Posted 19 March 2019 - 04:32 AM

someTabPage.Text
Was This Post Helpful? 0
  • +
  • -

#9 ZeevGoldstein   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-March 19

Re: tabcontrol - how to remove a tabpage by text ?

Posted 19 March 2019 - 04:51 AM

thank you
i am lost with it - your answer is not clear to me
tried all those options - nothig worked or produced error
i will try dig elsewhere
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6777
  • View blog
  • Posts: 27,959
  • Joined: 12-December 12

Re: tabcontrol - how to remove a tabpage by text ?

Posted 19 March 2019 - 04:55 AM

If you posted your code attempt you will likely receive some assistance.
Was This Post Helpful? 0
  • +
  • -

#11 maceysoftware   User is offline

  • Member Title
  • member icon

Reputation: 370
  • View blog
  • Posts: 1,586
  • Joined: 07-September 13

Re: tabcontrol - how to remove a tabpage by text ?

Posted 19 March 2019 - 06:37 AM

There is no built-in method to remove via caption/text.

What Andrew is saying is Loop around the tabpage's you have loaded, check the text matches the one you want to remove, then remove it {tabControl.TabPages.Remove(desiredTabPageTobeRemoved)}, by index {tabControl.TabPages.RemoveAt(desiredTabPageTobeRemoved.Index)} or by name {tabControl.TabPages.RemoveByKey(desiredTabPageTobeRemoved.Name)}.

All Andrew is asking for is your cooperation so we don't just hand you the answer and you learn.

If you want someone to just hand you the answer I will send you an invoice for my time, btw the lowest factor I charge by is the day.
Was This Post Helpful? 1
  • +
  • -

#12 ZeevGoldstein   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-March 19

Re: tabcontrol - how to remove a tabpage by text ?

Posted 19 March 2019 - 06:46 AM

thanks,
the penny dropped after this last reply
thank you all !!
Was This Post Helpful? 0
  • +
  • -

#13 DarenR   User is offline

  • D.I.C Lover

Reputation: 601
  • View blog
  • Posts: 3,933
  • Joined: 12-January 10

Re: tabcontrol - how to remove a tabpage by text ?

Posted 19 March 2019 - 09:46 AM

usually when you add a period after the item the intellisense will give you options


myBox.(intellisense turns on) box shows up with options
Was This Post Helpful? 0
  • +
  • -

#14 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 209
  • View blog
  • Posts: 1,187
  • Joined: 05-December 13

Re: tabcontrol - how to remove a tabpage by text ?

Posted 19 March 2019 - 11:51 AM

Firstly, lets declare a tab page and string
    Private myNewTab As TabPage
    Private tabpageName As String
With this button click event, I will populate the tabcontrol with tab pages ::
Private Sub PopulateBtn_Click(sender As Object, e As EventArgs) Handles PopulateBtn.Click
I will then loop from 0 to 6 which will give me 7 tabs since the indexing starts at zero. Next we set the text for tab page name and concatenate the string, and convert the int to string since that is what the tabpage requires when setting a name.
tabpageName = String.Concat("TabPage", Convert.ToString(i))
Create a new tab page and pass the tab name from the string we concatenated to it.
myNewTab = New TabPage(tabpageName)
We then pass some info into to a function tabExist . We will get to this soon. We then check that function to see if any other tabs contain that name by checking the tabpages text property of the collection of tab pages, and we do this by iteration.
            If Not tabExist(myNewTab, TabControl1, False) Then
                addTabPage(myNewTab)
            End If
Our function takes three arguments ::
Private Function tabExist(tp As TabPage, ctabControl As TabControl, Contains As Boolean) As Boolean
We check the newTP which is one of the tabpage from the collection of tab pages we will cycle through. We check the text property of the controls tabpage to see if it matches with the same name of the tabpage we are trying to create. If there is a duplicate it will exit for and report its existence. The reason I've explained how to avoid adding duplicates, is because its often a common question asked later. So maybe this will help you and future readers.
For Each newTP As TabPage In ctabControl.TabPages
            If newTP.Text.Contains(tp.Text) Then
                Contains = True
                Exit For
            End If
        Next
        Return Contains
If the condition of the function does not have a match, it will execute the following conditional statement
If Not tabExist(myNewTab, TabControl1, False) Then
                addTabPage(myNewTab)
            End If
inside of the PopulateBtn_Click event. This calls a sub called addTabPage and passes the tab name to it for creation. As you can see, addTabPage(myNewTab) takes one argument (myNewTab).

To remove a tabpage, you can do so by text or by the index it holds in the collection. Since you want to do it by name, here is an example ::
    Private Sub RemoveTabBtn_Click(sender As Object, e As EventArgs) Handles RemoveTabBtn.Click
        'Lets say we want to remove tabpage number 4
        For Each eTabPage As TabPage In TabControl1.TabPages
            If eTabPage.Text.Contains("TabPage4") Then
                TabControl1.TabPages.Remove(eTabPage)
            End If
        Next
    End Sub
As you can see, I iterate the collection of tab pages and then remove the tab page by name, but only if the name of the tab page matches the condition of the if statement.

Fully working and tested ::
    Private myNewTab As TabPage
    Private tabpageName As String
    Private Sub PopulateBtn_Click(sender As Object, e As EventArgs) Handles PopulateBtn.Click
        For i As Integer = 0 To 6
            tabpageName = String.Concat("TabPage", Convert.ToString(i))
            myNewTab = New TabPage(tabpageName)
            If Not tabExist(myNewTab, TabControl1, False) Then
                Console.WriteLine(String.Concat(myNewTab.Text, " does not exist, and will be added"))
                addTabPage(myNewTab)
            End If
        Next
    End Sub
    Private Function tabExist(tp As TabPage, ctabControl As TabControl, Contains As Boolean) As Boolean
        For Each newTP As TabPage In ctabControl.TabPages
            If newTP.Text.Contains(tp.Text) Then
                Contains = True
                Console.WriteLine(String.Concat(newTP.Text, " already exists and won't be added"))
                Exit For
            End If
        Next
        Return Contains
    End Function
    Private Sub addTabPage(tp As TabPage)
        Me.TabControl1.TabPages.Add(tp)
        Console.WriteLine(String.Concat(tp.Text, " Has been added"))
    End Sub
    Private Sub RemoveTabBtn_Click(sender As Object, e As EventArgs) Handles RemoveTabBtn.Click
        'Lets say we want to remove tabpage number 4
        For Each eTabPage As TabPage In TabControl1.TabPages
            If eTabPage.Text.Contains("TabPage4") Then
                TabControl1.TabPages.Remove(eTabPage)
                Console.WriteLine(String.Concat(eTabPage.Text, " Has been removed"))
            End If
        Next
    End Sub


Console Outputs like so ::
TabPage0 does not exist, and will be added
TabPage0 Has been added
TabPage1 does not exist, and will be added
TabPage1 Has been added
TabPage2 does not exist, and will be added
TabPage2 Has been added
TabPage3 does not exist, and will be added
TabPage3 Has been added
TabPage4 does not exist, and will be added
TabPage4 Has been added
TabPage5 does not exist, and will be added
TabPage5 Has been added
TabPage6 does not exist, and will be added
TabPage6 Has been added
TabPage0 already exists and won't be added
TabPage1 already exists and won't be added
TabPage2 already exists and won't be added
TabPage3 already exists and won't be added
TabPage4 already exists and won't be added
TabPage5 already exists and won't be added
TabPage6 already exists and won't be added
TabPage4 Has been removed

Was This Post Helpful? 1
  • +
  • -

#15 ZeevGoldstein   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-March 19

Re: tabcontrol - how to remove a tabpage by text ?

Posted 20 March 2019 - 01:17 AM

wow wow and once more time wow
thanks you so very much for this course
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2