|
-
Jun 3rd, 2014, 10:51 AM
#1
Thread Starter
Addicted Member
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.
-
Jun 3rd, 2014, 11:22 AM
#2
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.
-
Jun 3rd, 2014, 05:59 PM
#3
Re: Tabpage - Add or insert tabpage with order determined
 Originally Posted by dday9
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:
'Get the first TabPage with a greater Tag value than the one being inserted.
Dim followingTab = myTabControl.TabPages.Cast(Of TabPage)().FirstOrDefault(Function(tp) CInt(tp.Tag) > CInt(tabToInsert.Tag))
If followingTab Is Nothing Then
'There is no following tab so add this one to the end.
myTabControl.TabPages.Add(tabToInsert)
Else
'Insert this tab before the following one.
myTabControl.TabPages.Insert(myTabControl.TabPages.IndexOf(followingTab), tabToInsert)
End If
Last edited by jmcilhinney; Jun 3rd, 2014 at 06:05 PM.
-
Jun 4th, 2014, 07:20 PM
#4
Thread Starter
Addicted Member
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"?
-
Jun 4th, 2014, 08:42 PM
#5
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.
-
Jun 4th, 2014, 09:58 PM
#6
Re: Tabpage - Add or insert tabpage with order determined
 Originally Posted by Smartacus
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.
-
Jun 4th, 2014, 10:29 PM
#7
Thread Starter
Addicted Member
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
-
Jun 6th, 2014, 02:15 AM
#8
Thread Starter
Addicted Member
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)
-
Jun 9th, 2014, 08:39 AM
#9
Thread Starter
Addicted Member
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!!
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|