Results 1 to 9 of 9

Thread: Tabpage - Add or insert tabpage with order determined

  1. #1

    Thread Starter
    Addicted Member Smartacus's Avatar
    Join Date
    Oct 2009
    Location
    Deep South, USA
    Posts
    196

    Question Tabpage - Add or insert tabpage with order determined

    I have a main page (VB.Net 2012 4.0) which I have a treeview on the left that opens forms into a tab control page on the right. This all works fine except I would like to be able to determine the tab page display order when the treeview link is clicked. My example would be:

    Treview Item

    1: Demographics
    2: Intake
    3: Assessment(1)
    4: Assessment(2)
    5: Special Needs
    6: Outcome

    When the main form is opened, one tab page is created with a user control added as a facesheet that basically summarizes the users open tasks, the index is 0. After selecting the client filter they can then select a treeview item at random e.g. Item 4, Item 6, Item, Item 5, Item 1 and so on.

    My goal is to place the new tab page in the order which they should be read as 1,2,3,4,5 as it is being added so tab 5 would always be to the left of tab 6 etc.

    I have looked at dozens of examples and tried Insert instead of Add and giving the new tab page an pre-determined index but you can not add a tab with index 3 if it is greater than the collection.

    Help needed.

    Thanks.
    ***************************************************
    Smartacus comes packaged "As Is With No Warranty"

    ************* Useful Links ******************
    FAQs: Index / Database Development / .NET CodeBank /
    Before Posting Here...MSDN

    MZTools (I love this tool when using VB6 - Free) /

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Tabpage - Add or insert tabpage with order determined

    The way that I would do it is to set the Tag property of the tab page to whatever order it should be in. Then whenever you add a page, use the Add method, then iterate through each tab to check which order they should be in, and finally set the proper order then.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Tabpage - Add or insert tabpage with order determined

    Quote Originally Posted by dday9 View Post
    The way that I would do it is to set the Tag property of the tab page to whatever order it should be in. Then whenever you add a page, use the Add method, then iterate through each tab to check which order they should be in, and finally set the proper order then.
    I like the idea of using the Tag but don't Add and then re-order. Determine the correct order first and then use Insert. E.g.
    vb.net Code:
    1. 'Get the first TabPage with a greater Tag value than the one being inserted.
    2. Dim followingTab = myTabControl.TabPages.Cast(Of TabPage)().FirstOrDefault(Function(tp) CInt(tp.Tag) > CInt(tabToInsert.Tag))
    3.  
    4. If followingTab Is Nothing Then
    5.     'There is no following tab so add this one to the end.
    6.     myTabControl.TabPages.Add(tabToInsert)
    7. Else
    8.     'Insert this tab before the following one.
    9.     myTabControl.TabPages.Insert(myTabControl.TabPages.IndexOf(followingTab), tabToInsert)
    10. End If
    Last edited by jmcilhinney; Jun 3rd, 2014 at 06:05 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member Smartacus's Avatar
    Join Date
    Oct 2009
    Location
    Deep South, USA
    Posts
    196

    Re: Tabpage - Add or insert tabpage with order determined

    Code:
    Function(tp) CInt(tp.Tag) > CInt(tabToInsert.Tag)
    Do I need to enter the tag property as a string "1"?
    ***************************************************
    Smartacus comes packaged "As Is With No Warranty"

    ************* Useful Links ******************
    FAQs: Index / Database Development / .NET CodeBank /
    Before Posting Here...MSDN

    MZTools (I love this tool when using VB6 - Free) /

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Tabpage - Add or insert tabpage with order determined

    Yes, and then CInt converts it back to an Integer.

    Edit - I wasn't thinking. You could do it like that, or you could set it to just the number 1 as a Tag is of type Object. Either way would work, but just simply setting it to 1 is sufficient enough and a little less error prone.
    Last edited by dday9; Jun 4th, 2014 at 08:57 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Tabpage - Add or insert tabpage with order determined

    Quote Originally Posted by Smartacus View Post
    Code:
    Function(tp) CInt(tp.Tag) > CInt(tabToInsert.Tag)
    Do I need to enter the tag property as a string "1"?
    If you set a Tag property in the designer then it will be set to a String. If you set it in code then it will hold whatever type you assign. Either way though, you'll have to cast/convert to type Integer at run time when you use it, because Tag is type Object, so you'll be using CInt either way.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member Smartacus's Avatar
    Join Date
    Oct 2009
    Location
    Deep South, USA
    Posts
    196

    Re: Tabpage - Add or insert tabpage with order determined

    I do not have something right here.

    I am getting the behavior I expected with create a tabpage and adding form to it, and even my function to check for existing tabpage is working and not adding another tabpage if one by the same name already exists, but still not in order of TAG

    Code:
    'function Call
    ShowTabForm(frmIntake, "Intake")
    
     'Function to show a form in tabcontrol.
        Public Function ShowTabForm(ByVal FormName As Form, ByVal strTitle As String) As Boolean
    
            Dim rVal As Boolean = False
            Dim frm As New Form
            frm = FormName
            Dim tpp As New TabPage
            tpp.Tag = frm.Tag
            tpp.Name = frm.Name
    
            Dim strTabName As String = tpp.Name
            tpp.Text = strTitle
    
            If Not IsTabOpen(tpp.Name) Then
                'Get the first TabPage with a greater Tag value than the one being inserted.
                Dim followingTab = tbCtrl.TabPages.Cast(Of TabPage)().FirstOrDefault(Function(tp) CInt(tp.Tag) > CInt(frm.Tag))
                If followingTab Is Nothing Then
    
                    'There is no following tab so add this one to the end.
    
                    tbCtrl.TabPages.Add(tpp)
                    frm.TopLevel = False
                    frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
                    Me.tbCtrl.TabPages(tbCtrl.TabPages.IndexOf(tpp)).Controls.Add(frm)
    
                    Me.tbCtrl.SelectTab(tpp.Name)
                    frm.Show()
                Else
                    'Insert this tab before the following one.
                    tbCtrl.TabPages.Insert(tbCtrl.TabPages.IndexOf(followingTab), tpp.Tag)
                End If
            Else
                Me.tbCtrl.SelectTab(tpp.Name)
            End If
            Return rVal
        End Function
        'Determine if a formtab is already open.
        Public Function IsTabOpen(ByVal tabname As String) As Boolean
            Dim rVal As Boolean = False
            Dim tp As TabPage
            For Each tp In Me.tbCtrl.TabPages
                If tp.Name = tabname Then
                    rVal = True
                    Exit For
                End If
            Next
            Return rVal
        End Function
    ***************************************************
    Smartacus comes packaged "As Is With No Warranty"

    ************* Useful Links ******************
    FAQs: Index / Database Development / .NET CodeBank /
    Before Posting Here...MSDN

    MZTools (I love this tool when using VB6 - Free) /

  8. #8

    Thread Starter
    Addicted Member Smartacus's Avatar
    Join Date
    Oct 2009
    Location
    Deep South, USA
    Posts
    196

    Re: Tabpage - Add or insert tabpage with order determined

    I am assuming that this is where I am getting it wrong. But I have switched things around and still not inserting before other tabs if tag value is less than the ones on right.

    Code:
     'Insert this tab before the following one.
                    tbCtrl.TabPages.Insert(tbCtrl.TabPages.IndexOf(followingTab), tpp.Tag)
    ***************************************************
    Smartacus comes packaged "As Is With No Warranty"

    ************* Useful Links ******************
    FAQs: Index / Database Development / .NET CodeBank /
    Before Posting Here...MSDN

    MZTools (I love this tool when using VB6 - Free) /

  9. #9

    Thread Starter
    Addicted Member Smartacus's Avatar
    Join Date
    Oct 2009
    Location
    Deep South, USA
    Posts
    196

    Re: Tabpage - Add or insert tabpage with order determined

    I have checked my tags and they are numbered starting at 0, I removed the first default tab so the menu click now creates the first tab index. Still not loading in order I want. HELP!!
    ***************************************************
    Smartacus comes packaged "As Is With No Warranty"

    ************* Useful Links ******************
    FAQs: Index / Database Development / .NET CodeBank /
    Before Posting Here...MSDN

    MZTools (I love this tool when using VB6 - Free) /

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width