Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

Just having an issue that has completely stumped me.

In a nutshell, how to hide/show certain controls when a tabpage is changed?
ie. when the tabpage 1 is switched to tabpage 2 by the users, I want to hide the menustrip and toolbar.

Cheers
Siek
Posted
Updated 27-Jan-11 7:40am
v2

The TabControl object has an event called Selected.

When a new tab is selected, this event fires. One of the objects included in the event's arguments is the TabPage that was just selected.

So, in the method, you can check e.TabPage.Name and see if you want to show or hide the controls.
 
Share this answer
 
Comments
SIFNOk 27-Jan-11 13:51pm    
Thanks William, Theroically i understand what you are saying but unfortantly i am unable to find the "Selected" event.
fjdiewornncalwe 27-Jan-11 14:55pm    
Check KSchuler's answer. He's got the right event identified.
William Winner 27-Jan-11 16:09pm    
Well, just like with any control, there are multiple events that one could use. If you look at the code for the TabControl, specifically the WmSelChange() method, what you will find is that OnSelected is fired immediately before OnSelectedIndexChanged. And with the SelectedIndexChanged event, you don't get anything in your arguments.

But with the Selected event, you get passed not only the SelectedTab object, but also the SelectedIndex number. This means that the SelectedTab event is a more self-contained solution and a better one.
William Winner 27-Jan-11 16:12pm    
The Selected event is under the category of Action. Or, you can switch to the alphabetical view and find it right after RightToLeftLayoutChanged.
I use the TabControl's SelectedIndexChanged Event. Make sure you are looking at events for the TabControl not the TabPage. You can include code that checks which TabPage is selected like this:

VB
Private Sub tcMyTabControl_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tcMyTabControl.SelectedIndexChanged
       'If newly selected tab is tabpage2
       If tcMyTabControl.SelectedTab Is tpTabPage2 Then
          'Hide menu/toolbar
       Else
          'Show menu/toolbar           
       End If
End Sub


Hope this helps.
 
Share this answer
 
Comments
fjdiewornncalwe 27-Jan-11 14:55pm    
+5. My choice for the correct answer.
Espen Harlinn 27-Jan-11 15:59pm    
I agree :)
Espen Harlinn 27-Jan-11 15:59pm    
5+ Good answer
Sergey Alexandrovich Kryukov 27-Jan-11 16:22pm    
This is the right one - a 5.
--SA
William Winner 27-Jan-11 17:59pm    
What exactly makes it the "right" one. As far as I can see, using SelectedIndexChanged or Selected is personal preference and therefore there is no right or wrong!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900