Menustrip and TabControl next and previous

antrew80

Member
Joined
Jul 1, 2014
Messages
8
Programming Experience
Beginner
Hi guys,
i am new to VB and i have the follow question.
I have a menu strip with next and previous buttons
i also have a tab control with several tabs and i want to navigate to each tab with my next and previous button from the menu strip
if i use
VB.NET:
 TabControl1.SelectTab(1)
i go to the next page and if i use
VB.NET:
 TabControl1.SelectTab(0)
i go to the previous page.
what about if i want to move to the 3rd page and go on
attached is a screen shot of my program.is in greek but you can get the general point

thanks
 

Attachments

  • menustrip.jpg
    menustrip.jpg
    132.3 KB · Views: 76
Hi,

Just create a Private Class Level / Member variable to hold the current index of the page that you are on and then increment or decrement that variable each time you want to move up or down a Tab page. You then set the SelectTab Method of the Tab Control to that index. Just make sure that you check to make sure you are not going beyond the Lower and Upper bound of the Tab page index first which will be set at Zero and TabControl1.TabPages.Count – 1.

Hope that helps.

Cheers,

Ian
 
hi Ian can you please help me with the code because i dont know how to do your suggestion.
not all the code. at least how to do the class level / mebmer.

thanks a lot
 
Hi Ian,
i found another way to it but i have a problem so maybe you can help me also on this
VB.NET:
 TabControl1.SelectTab(1)
         If TabControl1.SelectedIndex = 3 Then
        TabControl1.SelectTab(+1)

i use the code above to go the next tab but when i am in 4th tab and i press next button it goes to 2 tab and not in the 5th.
any idea why?

thanks also for your help
 
Hi,

i found another way to it but i have a problem so maybe you can help me also on this

No, I will only help you to do it the right way and not some concocted method you seem to have come up with using +1???? Using +1 is the same as saying 1.

Please read up on the SelectTab method of the TabControl to see what the Method is expecting to see why your solution does not work:-

TabControl.SelectTab Method (Int32) (System.Windows.Forms)

Cheers,

Ian
 
hi Ian
the solution that i did is the following and works fine
VB.NET:
        If TabControl1.SelectedIndex = 0 Then
            TabControl1.SelectTab(1)


        ElseIf TabControl1.SelectedIndex = 1 Then
            TabControl1.SelectTab(2)

i am sure that must be more easy way but its ok for me and this solution. i just want to finish the project

thanks for your help
 
i am sure that must be more easy way but its ok for me and this solution. i just want to finish the project

OK, fair enough, but you are going about this all wrong. What if you had 100 Tab Pages? How many IF statements are you going to need to go to the next Tab Page and how many IF statements are you going to need to go to the previous Tab Page? In addition to this what happens if you add a New Tab Page After your code is written? Your code will not work for the new Tab Pages unless you rewrite your code.

So, What I was trying to teach you? Create a Class Level Variable, i.e:-
Public Class Form1
  'This is a Private variable Declared at the Class Level
  Private currentTabPageSelected As Integer = 0
 
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 
  End Sub
End Class


Then, if you want to move to the next tab page, check you are NOT going beyond the Upper Bound of the number of Tab Pages in the Tab Control and then increment that variable by 1 and then set the Next Tab Page. i.e:-
Private Sub btnGotoNextTabPage_Click(sender As Object, e As EventArgs) Handles btnGotoNextTabPage.Click
  If Not currentTabPageSelected = TabControl1.TabPages.Count - 1 Then
    currentTabPageSelected += 1
    TabControl1.SelectTab(currentTabPageSelected)
  End If
End Sub


However, if you want to move to the previous tab page, check you are NOT going beyond the Lower Bound of the number of Tab Pages in the Tab Control (being Zero) and then decrement that variable by 1 and then set the Next Tab Page. i.e:-
Private Sub btnGotoPreviousTabePage_Click(sender As Object, e As EventArgs) Handles btnGotoPreviousTabePage.Click
  If Not currentTabPageSelected = 0 Then
    currentTabPageSelected -= 1
    TabControl1.SelectTab(currentTabPageSelected)
  End If
End Sub


I hope this helps you to learn how to use the information / advice you are given in any future Posts that you may make.

Cheers,

Ian

NB. If you want a challenge:-

How can you TELL ME that I am being inefficient in my examples above? How could you rewrite the above code to get rid of the Private Variable that I am using and use the SelectedIndex Property of the Tab Control instead?
 
Good Morning Ian,
i never told you that you were inefficient.
instead of that you were trying to teach me how to code.
Thanks a lot for your help
 
Back
Top