7

I am using a For Each loop:

For Each tab As TabPage in TabControl1.TabPages

I cannot retrieve the index of tab. Is there a way to do this? I am trying to see if the index is below or above the current index.

0

2 Answers 2

19

You can use the method IndexOf of the TabPageCollection to get the Index of a Tab.

Dim indexOfTab As Integer = TabControl1.TabPages.IndexOf(tab)
2
  • Same as pluto - I wish I knew this before! :) thanks so much. Oct 14, 2013 at 12:53
  • Using a For loop (not for each) seemed to solve my problem, but thanks anyway! Oct 14, 2013 at 12:54
3

There's also

Dim indexOfTab As Integer = TabControl1.TabPages.IndexOfKey("TabName")

or

Dim indexOfTab As Integer = TabControl1.TabPages.IndexOfKey(tab.Name)

This is useful if the tab page is ever removed from the collection but you want to know where it was. I know this sounds esoteric, but I've had that need quite a few times, and you can't get this information from the tab page object itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.