Enable Disable TabPage inside TabControl in Windows Application using C# and VB.Net

Akram.19
 
on Jun 22, 2022 11:16 PM
Sample_114879.zip
241 Views

I have a tabcontrol1 with two tab pages.

First, I left the first tab as default and disabled the second tab.

I have a button in the first tab, I want when I click that button, the second tab must be enabled.

I will upload the code.

Private PreviousTab As New TabPage
Private CurrentTab As New TabPage

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

Private Sub TabControl1_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
    CurrentTab = e.TabPage
    If (PreviousTab.Name <> CurrentTab.Name) And (CurrentTab.Name = TabPage2.Name) Then
        TabControl1.SelectedTab = PreviousTab
    End If

Private Sub cmdlogin_Click(sender As Object, e As EventArgs) Handles cmdlogin.Click
    If SQL.HasConnection = True Then
        ' MsgBox("SUCCESS!")
        If IsAuthenticated() = True Then
            AuthUser = txtuser.Text
            GetUserInfo()
            CheckBox15.Checked = True
            Dim tabPage As TabPage
            For Each tabPage In TabControl1.TabPages
                If tabPage.Text = "TabPage2" Then
                    TabControl1.SelectedTab = TabPage2
                    tabPage.Enabled = True
                 End If
            Next
            End If
        End If
    End Sub
Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Jun 23, 2022 12:53 AM
on Jun 23, 2022 12:56 AM

Hi Akram.19,

Please use TabPage Proprty of TabContrrol which represents a page of the TabControl that can host child controls, then set enable or disable as per your requirement.

Inside the Tab 2 add Panel control and place all the controls inside the Panel.

In Form Load set the Panel Visible property to false.

The on Button click in Tab 1 set the Visible property to true.

Please refer below sample.

Code

C#

private void Form1_Load(object sender, EventArgs e)
{
    ((TabPage)tabAbout).Enabled = false;
    panel1.Visible = false;
}

private void EnableTab(object sender, EventArgs e)
{
    tabControl1.SelectedTab = ((TabPage)tabAbout);
    ((TabPage)tabAbout).Enabled = true;
    panel1.Visible = true;
}

VB.Net

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    CType(tabAbout, TabPage).Enabled = False
    Panel1.Visible = False
End Sub

Private Sub EnableTab(ByVal sender As Object, ByVal e As EventArgs) Handles btnEnableTab.Click
    tabControl1.SelectedTab = (CType(tabAbout, TabPage))
    CType(tabAbout, TabPage).Enabled = True
    Panel1.Visible = True
End Sub

Screenshot