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?
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?
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
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.
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
You can disabled a tab by setting its Enabled
property:
TabControl1.TabPages("tbPage1").Enabled = False