none
How to: Disable Tab Pages in TabControl from another Form? RRS feed

  • Question

  • Hi EveryOne,

    I have a Tabcontrol in Form2 with 4 TabPages (tabPage1,tabPage2,tabPage3,tabPage4), 

    I want to Disable (tabPage2,tabPage3,tabPage4) once clicking button(  btnGetInfo) From Form1.

    I tryed This Samples of Code but It doesn't work,

    So  I am counting on your Help for to solve this problem,

    Thank you Very Much:

        Private Sub btnGetInfo_Click(sender As Object, e As EventArgs) Handles btnGetInfo.Click
    Dim frm As New Form2
    Dim tabpage1, tabPage2, tabPage3, tabPage4 As New TabPage
    frm.ShowDialog()
    frm.TabControl1.SelectedTab = tabpage1
    frm.TabControl1.TabPages(2).Enabled = False
    frm.TabControl1.TabPages(3).Enabled = False
    frm.TabControl1.TabPages(4).Enabled = False
    End Sub



    • Edited by Bajtitou Friday, March 30, 2018 9:45 PM
    Friday, March 30, 2018 9:40 PM

Answers

  • Example:

    Public Class Form1
        Friend WithEvents Button1 As New Button With {.AutoSize = True, .Text = "Hide Tabs"}
    
        Private tabForm As Form2
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Controls.Add(Button1)
            tabForm = New Form2
            tabForm.Show()
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For i = 1 To 3
                tabForm.TabControl1.TabPages(i).Enabled = False
            Next
        End Sub
    End Class
    
    Public Class Form2
        Friend WithEvents TabControl1 As New TabControl With {.Dock = DockStyle.Fill}
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Controls.Add(TabControl1)
            TabControl1.TabPages.AddRange({New TabPage, New TabPage, New TabPage, New TabPage})
        End Sub
    End Class
    It may be that Enabled isn't actually what you want and removing/re-adding the tabpages may be preferable.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Bajtitou Saturday, March 31, 2018 8:21 AM
    Friday, March 30, 2018 10:05 PM
    Moderator
  • Example:

    It may be that Enabled isn't actually what you want and removing/re-adding the tabpages may be preferable.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Hi, 

    This Example Is the same of mine it doesn't disable tabPages Too.

    I tryed Removing with This methode and I get no result.

    frm.TabControl1.TabPages.Remove(tabPage2)
    frm.TabControl1.TabPages.Remove(tabPage3) 
    frm.TabControl1.TabPages.Remove(tabPage4)

    Thank you ,Regards , Bajtitou.

    Hello,

    Take note that Reed uses Show rather than ShowDialog. The main difference is that when using ShowDialog you are in a modal state while what Reed has shown is non-modal which as I see it will work.

    So the highlighted code will not execute until after the form is closed.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Bajtitou Saturday, March 31, 2018 8:21 AM
    Saturday, March 31, 2018 1:07 AM
    Moderator
  • In regards to disabling all tab, first off my initial thoughts are to use tabs enabled/disabled would be in a process where you move from tab to tab forward motion e.g. do something on tab1, move to tab2 etc. Usually logic permits moving backwards but not skipping a tab.

    In the following example done on the form with the tab control (we could do this on the calling form too but that requires rigging up more logic and personally does not make sense yet it may to you do go for it if that is what you truly want.

    Public Class Form2
        Private _tabIndex As Integer = 0
        ''' <summary>
        ''' Set next valid tab
        ''' 
        ''' only allow next tab to move forward unless
        ''' _tabIndex equals -1
        ''' 
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub allTabButtons_Click(sender As Object, e As EventArgs)
    
            if _tabIndex > -1
                If _tabIndex < tabControl1.TabCount - 1 Then
                    _tabIndex += 1
                    tabControl1.SelectedIndex = _tabIndex
                End If
            End If
    
        End Sub
        ''' <summary>
        ''' only allow next tab to move forward unless
        ''' _tabIndex equals -1
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub TabControl1_Selecting(sender As Object, e As TabControlCancelEventArgs) _ 
            Handles TabControl1.Selecting
    
            if _tabIndex > -1
                If e.TabPageIndex <> _tabIndex
                    e.Cancel = True
                End If
            Else 
                 e.Cancel = True
            End If
    
    
        End Sub
        ''' <summary>
        ''' Get all buttons on each tab page, hook them up to a single click event
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            for each tab as TabPage In TabControl1.Controls
                tab.Controls.OfType(of button).ToList().ForEach(
                    Sub(b) AddHandler b.Click, AddressOf allTabButtons_Click)
            Next
    
        End Sub
        ''' <summary>
        ''' Restart at first tab
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub cmdRestart_Click(sender As Object, e As EventArgs) _ 
            Handles cmdRestart.Click
    
            _tabIndex = 0
            TabControl1.SelectedTab = TabPage1
    
        End Sub
        ''' <summary>
        ''' Dieable all tabs
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub cmdDisableAll_Click(sender As Object, e As EventArgs) _ 
            Handles cmdDisableAll.Click
    
            _tabIndex = -1
    
        End Sub
    End Class
    

    As coded, Restart goes back to the first tab, disable all does not change the current tab but stays on the current tab and stops selection of tabs.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Bajtitou Saturday, March 31, 2018 6:58 PM
    Saturday, March 31, 2018 10:07 AM
    Moderator

All replies

  • That code creates a new instance of Form2 which is not the same as the instance you have already shown.

    Where did you show Form2 from the code in Form1?

    Go to that spot in Form1 and make sure you are saving a class level reference to the instance of Form2 that you create and show.  Then you can access the TabControl from that variable on Form1.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, March 30, 2018 9:57 PM
    Moderator

  • Where did you show Form2 from the code in Form1?

    Hi, 

    I show Form2 from here:

    Dim frm As New Form2
    frm.ShowDialog()

    Thanks , Bajtitou

    Friday, March 30, 2018 10:03 PM
  • Example:

    Public Class Form1
        Friend WithEvents Button1 As New Button With {.AutoSize = True, .Text = "Hide Tabs"}
    
        Private tabForm As Form2
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Controls.Add(Button1)
            tabForm = New Form2
            tabForm.Show()
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For i = 1 To 3
                tabForm.TabControl1.TabPages(i).Enabled = False
            Next
        End Sub
    End Class
    
    Public Class Form2
        Friend WithEvents TabControl1 As New TabControl With {.Dock = DockStyle.Fill}
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Controls.Add(TabControl1)
            TabControl1.TabPages.AddRange({New TabPage, New TabPage, New TabPage, New TabPage})
        End Sub
    End Class
    It may be that Enabled isn't actually what you want and removing/re-adding the tabpages may be preferable.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Bajtitou Saturday, March 31, 2018 8:21 AM
    Friday, March 30, 2018 10:05 PM
    Moderator
  • Example:

    It may be that Enabled isn't actually what you want and removing/re-adding the tabpages may be preferable.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Hi, 

    This Example Is the same of mine it doesn't disable tabPages Too.

    I tryed Removing with This methode and I get no result.

    frm.TabControl1.TabPages.Remove(tabPage2)
    frm.TabControl1.TabPages.Remove(tabPage3) 
    frm.TabControl1.TabPages.Remove(tabPage4)

    Thank you ,Regards , Bajtitou.

    Friday, March 30, 2018 10:37 PM
  • Example:

    It may be that Enabled isn't actually what you want and removing/re-adding the tabpages may be preferable.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Hi, 

    This Example Is the same of mine it doesn't disable tabPages Too.

    I tryed Removing with This methode and I get no result.

    frm.TabControl1.TabPages.Remove(tabPage2)
    frm.TabControl1.TabPages.Remove(tabPage3) 
    frm.TabControl1.TabPages.Remove(tabPage4)

    Thank you ,Regards , Bajtitou.

    Hello,

    Take note that Reed uses Show rather than ShowDialog. The main difference is that when using ShowDialog you are in a modal state while what Reed has shown is non-modal which as I see it will work.

    So the highlighted code will not execute until after the form is closed.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Bajtitou Saturday, March 31, 2018 8:21 AM
    Saturday, March 31, 2018 1:07 AM
    Moderator

  • Hello,

    Take note that Reed uses Show rather than ShowDialog. The main difference is that when using ShowDialog you are in a modal state while what Reed has shown is non-modal which as I see it will work.

    So the highlighted code will not execute until after the form is closed.


    Hello Karen,

    You're absolutely right, showdialog () prevent code execution.

    It is allright now.

    Thank you very much for this Note.

    Is there a way to disable only the tabs??

    Best Regards, Bajtitou.



    • Edited by Bajtitou Saturday, March 31, 2018 8:59 AM
    Saturday, March 31, 2018 8:27 AM
  • In regards to disabling all tab, first off my initial thoughts are to use tabs enabled/disabled would be in a process where you move from tab to tab forward motion e.g. do something on tab1, move to tab2 etc. Usually logic permits moving backwards but not skipping a tab.

    In the following example done on the form with the tab control (we could do this on the calling form too but that requires rigging up more logic and personally does not make sense yet it may to you do go for it if that is what you truly want.

    Public Class Form2
        Private _tabIndex As Integer = 0
        ''' <summary>
        ''' Set next valid tab
        ''' 
        ''' only allow next tab to move forward unless
        ''' _tabIndex equals -1
        ''' 
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub allTabButtons_Click(sender As Object, e As EventArgs)
    
            if _tabIndex > -1
                If _tabIndex < tabControl1.TabCount - 1 Then
                    _tabIndex += 1
                    tabControl1.SelectedIndex = _tabIndex
                End If
            End If
    
        End Sub
        ''' <summary>
        ''' only allow next tab to move forward unless
        ''' _tabIndex equals -1
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub TabControl1_Selecting(sender As Object, e As TabControlCancelEventArgs) _ 
            Handles TabControl1.Selecting
    
            if _tabIndex > -1
                If e.TabPageIndex <> _tabIndex
                    e.Cancel = True
                End If
            Else 
                 e.Cancel = True
            End If
    
    
        End Sub
        ''' <summary>
        ''' Get all buttons on each tab page, hook them up to a single click event
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            for each tab as TabPage In TabControl1.Controls
                tab.Controls.OfType(of button).ToList().ForEach(
                    Sub(b) AddHandler b.Click, AddressOf allTabButtons_Click)
            Next
    
        End Sub
        ''' <summary>
        ''' Restart at first tab
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub cmdRestart_Click(sender As Object, e As EventArgs) _ 
            Handles cmdRestart.Click
    
            _tabIndex = 0
            TabControl1.SelectedTab = TabPage1
    
        End Sub
        ''' <summary>
        ''' Dieable all tabs
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub cmdDisableAll_Click(sender As Object, e As EventArgs) _ 
            Handles cmdDisableAll.Click
    
            _tabIndex = -1
    
        End Sub
    End Class
    

    As coded, Restart goes back to the first tab, disable all does not change the current tab but stays on the current tab and stops selection of tabs.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Bajtitou Saturday, March 31, 2018 6:58 PM
    Saturday, March 31, 2018 10:07 AM
    Moderator
  • Example:

    It may be that Enabled isn't actually what you want and removing/re-adding the tabpages may be preferable.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Hi, 

    Thank you so much,

    frm.ShowDialog()

     showdialog () prevent code execution.

    It is allright now.

    Is there a way to disable only the tabs??

    Best Regards ,Bajtitou.


    Saturday, March 31, 2018 12:51 PM
  • Example:

    It may be that Enabled isn't actually what you want and removing/re-adding the tabpages may be preferable.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Hi, 

    Thank you so much,

    frm.ShowDialog()

     showdialog () prevent code execution.

    It is allright now.

    Is there a way to disable only the tabs??

    Best Regards ,Bajtitou.


    I already responded to disabling tabs.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, March 31, 2018 1:47 PM
    Moderator

  • I already responded to disabling tabs.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Hello,

    Thanks for all,

    I tested this Samples .

    Now I'm working on this code and see what I'm going to do with it to improve my project.

    Best Regards , Bajtitou.


    Saturday, March 31, 2018 7:18 PM