1

I have a tabControl in my vb.net application - that has 3 tabs. Upon a click on the first tab I'm trying to do something so that user will not be able to get out of that tab - which seems to be a lot more difficult that I anticipated. I'm simply trying to disable the TAB itself - so that they are unable to leave the current tab - the TABPAGE does not have to be disabled since they are not supposed to be able to get out of the current one...

i'm trying something along the way of....

tabControl1.tabPage(1).enabled=false

and

tabcontrol1.tabpage1.enabled=false

and even trying to hide it

tabcontrol1.tabpage(1).visible=false

And nothing seems to work.........!

i've even tried

tabPage1.hide()

But doesn't do anything

EDIT:

I found this code - will I have to do something with this in order to disable the actual TAB - Not TAB PAGE - I don't want user leaving the tab they've on when they click a specific button...

   Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem

    Dim g As Graphics
    Dim sText As String
    Dim iX As Integer
    Dim iY As Integer
    Dim sizeText As SizeF
    Dim ctlTab As TabControl
    ctlTab = CType(sender, TabControl)
    g = e.Graphics

    sText = ctlTab.TabPages(e.Index).Text

    sizeText = g.MeasureString(sText, ctlTab.Font)

    iX = e.Bounds.Left + 6

    iY = e.Bounds.Top + (e.Bounds.Height - sizeText.Height) / 2

    g.DrawString(sText, ctlTab.Font, Brushes.Black, iX, iY)

End Sub

Found this here...http://www.dreamincode.net/forums/topic/125792-how-to-make-vertical-tabs/

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
BobSki
  • 1,531
  • 2
  • 25
  • 61
  • I recommend using `Option Explicit On` because the lowercase letters after the dots look suspicious. I think it's something like `tabControl1.TabPages(1).Enabled=False` – Slai Nov 15 '16 at 20:58
  • @slai i retyped code here from my application and didn't copy/paste – BobSki Nov 15 '16 at 20:59
  • 1
    Won't say it is the right way, but I just force them to whatever tab I want them to be on with `.SelectedTab` property. – topshot Nov 15 '16 at 21:00
  • @slai if you look at my Q - it shows the same exact code and I also explain that it doesn't work – BobSki Nov 15 '16 at 21:02
  • Will i have to create my own tabs with Owner Fixed? – BobSki Nov 15 '16 at 21:29
  • I didn't see the `s` in the ones that you tried, but I tried them too and none of them worked. It's mentioned in the [`TabPage` Remarks section](https://msdn.microsoft.com/en-us/library/system.windows.forms.tabpage#Anchor_7) : "Additionally, the Hide method of the TabPage will not hide the tab. To hide the tab, you must remove the TabPage control from the TabControl.TabPages collection." http://stackoverflow.com/questions/3365025/hiding-and-showing-tabpages-in-tabcontrol – Slai Nov 15 '16 at 23:02
  • 2
    You just need to implement an event handler for the TabControl's Selecting event. Set e.Cancel = True to stop it. Making it obvious to the user that he can't change the tab so he won't wildly bang at the other tabs in frustration is up to you. – Hans Passant Nov 15 '16 at 23:11
  • @HansPassant - this works great - that's all I really needed. Please post as answer and I will gladly accept it! – BobSki Nov 16 '16 at 14:04
  • 1
    Rather best if you post it yourself so it matches your question well. Just show what code you used, mark the post as the answer to close your question. – Hans Passant Nov 16 '16 at 14:18

2 Answers2

2

With the help of @Hans Passant I did this to resolve this problem....

Private Sub TabControl1_Selecting(sender As Object, e As TabControlCancelEventArgs) Handles TabControl1.Selecting

        e.cancel=True

End Sub

In my case this works perfectly........

BobSki
  • 1,531
  • 2
  • 25
  • 61
1

They hide Enabled in the property window, but it does partially work. It wont disable the TabPage but as a container control, it does disable all the child controls. The "trick" then becomes how to convey to the user that this or that tab is available: for that, use the image properties:

TabControl1.TabPages(1).Enabled = False
TabControl1.TabPages(1).ImageIndex = 1

Result:

enter image description here

You can embellish to make it clear, for instance a label with "Step 1 must be completed first". You can still intervene to stop the tab change with the SelectedIndexChanged event but since they cannot interact with any control, there is no real need.

You can also use the Image part with the SelectedIndexChanged event trap, as a means to tell them it is not available.


There is yet another way but the other alteratives are simpler. This can work ok for a Wizard scenario with a "Next >>" type button

  • Create a List(of TabPage)
  • Store pages 1-N in it
  • Remove Pages 1-N from the TabControl (leaving TabPage(0))
  • When they hit 'Next`, add the next page back to the control

Example:

Select Case TabControl1.TabPages.Count
    Case 1
        TabControl1.TabPages.Add(Pgs(0))
    Case 2
        TabControl1.TabPages.Add(Pgs(1))
    ...
End Select

If there is a "New Foo" button to wizard thru the steps for a new Foo, just remove pages 1-N again. I am not fond of controls coming and going visibility wise, but it can useful when Step Two can vary depending on a value in Step 1 (ie either of TabPage 2, 3 or 4 can be used for Step 2 depending...).

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178