views:

585

answers:

4

Hi, I have a TabControl with two TabPages and I was wondering what is the best way to test which tab is currently displayed? I'm not sure why I can't figure this one out...

+3  A: 

TabControl.SelectedTab

Austin Salonen
Thanks. This is kinda ugly but it works.
Cody C
+1  A: 

Assuming this is a WPF application, make sure that each TabItem has an Name.

Then it's just a matter of checking.

if tabItem1.IsSelected = true then
  ' Do Something 
else if tabItem2.IsSelected = true then
  ' Do Something 
end if
Stephen Wrighton
Yeah, this is how I was figuring how to do it but there's no members called IsSelected or Selected, I guess you have to use SelectedTab, kinda ugly but it works.
Cody C
doing a bit more lokoing into this, apparently the IsSelected is only available for the WPF TabItem class. The WinForms one doesn't support the IsSelected property.
Stephen Wrighton
+1  A: 
TabControl.SelectedTab.

Here's the link.

Ngu Soon Hui
A: 

If you use .Net 3.5, you can create a IsSelected method as an extension method if you wish:

Public Module TabControlExtensions
    <Extension()> _
    Public Function IsSelected(ByVal tabPage As TabPage) As Boolean
        Dim tabControl = CType(tabPage.Parent, TabControl)
        Return (tabControl.SelectedTab Is tabPage)
    End Function
End Module
Meta-Knight