3

I currently have a form that uses TabControl which has 5 TabPages. I want to create a button that could disable a specific TabPage.

I have tried

TabPage1.Enabled = False

But it does not work. How do I do this?

nathanchere
  • 8,008
  • 15
  • 65
  • 86
Marc Intes
  • 737
  • 9
  • 25
  • 51
  • possible duplicate of [How can I disable a tab inside a TabControl?](http://stackoverflow.com/questions/418006/how-can-i-disable-a-tab-inside-a-tabcontrol) – Jeremy Oct 03 '13 at 06:25
  • 1
    can you try this one Me.TabControl1.TabPages(1).Enabled = False – Pradip Oct 03 '13 at 06:27
  • After you disable it, perhaps make it invisible? `Visible = False` – Jeremy Oct 03 '13 at 06:27

4 Answers4

11

you need to use the TabPages collection. Add a button to your form and try this

Private Sub Button1_Click( sender As Object,  e As EventArgs) Handles Button1.Click
        TabControl1.TabPages(0).Enabled =false
End Sub

It's a base zero array, so in your case it should be from 0-4.

Or you can access it from the text of the tab

Private Sub Button2_Click( sender As Object,  e As EventArgs) Handles Button2.Click
    Dim tabPage As TabPage

    For Each tabPage In TabControl1.TabPages
        If tabPage.Text ="TabPage2"
            tabPage.Enabled =False
        End If
    Next
End Sub
Archlight
  • 2,019
  • 2
  • 21
  • 34
  • That does a bit different thing - it disables a *content* of the `TabPage`, not the `TabPage` itself. The page remains enabled. Nice to know anyway, might get useful. – Oak_3260548 Sep 08 '22 at 08:44
4

Currently, the following two code blocks does the same thing: disables all the controls on that TabPage (Sets Control.Enabled = False). The tab itself is still visible and selectable from the TabControl, it is not hidden. The tab is selectable and all the elements appear disabled.

TabMyTab.Enabled = False

MyTabControl.TabPages(4).Enabled = False where the TabPages(4) is the 5th in the TabControl collection.

Your initial code should work if that is your intent.

If you want to disable the tab similar to i.e. button.Enabled = False which does not allow the control to be used, you will need to do something different as disabling a TabPage as in the code above disables all controls in that tab. If this is what you want, keep reading. A lot of programmers suggest using the TabControl to disallow the tab from being selected by selecting a different or the previously selected tab. This is the most effective way I know. I would implement this as follows:

Private PreviousTab As New TabPage
Private CurrentTab As New TabPage

Private Sub TabControlName_Deselected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControlName.Deselected
        PreviousTab = e.TabPage
End Sub

Private Sub TabControlName_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControlName.Selected
        CurrentTab = e.TabPage
        If (PreviousTab.Name <> CurrentTab.Name) And (CurrentTab.Name = UnselectableTab.Name) Then
            MessageBox.Show("Tab disabled.", "Selection Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            TabControlName.SelectedTab = PreviousTab
        End If
End Sub

Substitute your own values for "UnselectableTab" and "TabControlName" for your project.

j3josh6
  • 51
  • 4
  • I think there're a couple of issues here, not the least being the ambiguity of "Enabled" and the fact that the property is marked "This member is not meaningful for this control"! I generally use this to disable all the controls on the tab, for example, if a license has expired. And it works either directly - `myTab.Enabled = True|False` - or using the index as @j3osh6 says. – SteveCinq Aug 29 '18 at 00:44
2

You can combine the use of disabling the tab, that way the behavior is dynamic if you change which tabs are enabled or disabled in code.

Private Sub TabControl1_Deselected(sender As Object, e As TabControlEventArgs) Handles TabControl1.Deselected
    PreviousTab = e.TabPage
End Sub

.

Private Sub TabControl1_Selected(sender As Object, e As TabControlEventArgs) Handles TabControl1.Selected
    If Not e.TabPage.Enabled Then
        TabControl1.SelectedTab = PreviousTab
    End If
End Sub
Sevvn
  • 21
  • 2
0

You can disabled a tab by setting its Enabled property:

TabControl1.TabPages("tbPage1").Enabled = False

laylarenee
  • 3,276
  • 7
  • 32
  • 40
anpadia
  • 111
  • 4