Question Really strange tabcontrol behavior

katz

Member
Joined
May 24, 2013
Messages
6
Programming Experience
Beginner
There's some very strange stuff going on here that I can't even start to work out. Bear with me, as this is rather long.

I need to be able to toggle fields between being disabled and being invisible, and I've got a little subroutine that does it recursively for each control and its children:

VB.NET:
    Public Sub ConcealObject(ByVal Parent As Control)
            For Each C As Control In Parent.Controls
            If C.Enabled = False OrElse C.Visible = False Then
                 Conceal(C) 'this just disables (and reveals) or hides (and enables) a control, depending on the setting
            ElseIf C.HasChildren Then
                ConcealObject(C) 'repeats the loop at the next level down (will keep repeating for as many levels as there are)
            End If
        Next C
    End Sub

So when that runs, it'll either turn all the disabled fields to invisible (but enabled) or turn all the invisible fields to diabled (but visible).

It works for most things, but tabcontrols completely mess it up:

1. The program starts out with some fields disabled; when I switch them to invisible, the fields on the tab page that has focus turn invisible, but the fields on the other pages stay disabled.

2a. Then, when I switch back to disabled (keeping focus on the same tab page), the tab page with focus still works correctly, but all the controls on the other pages are disabled, not just the ones that were previously invisible.

2b. But here's the really inexplicable (to me) part: If, instead of keeping focus on the same page when I switch back to disabled, I click on a different tab page (one of the pages where the disabled fields should have been hidden, but weren't) and then I switch back to disabled, then all the contents of that tab page disappear and are replaced by the contents of the first tab page. The page with index 0. All disabled.

3b. If I then click on a different tab (including the one with index 0), it will show the correct controls at least (but all disabled), and then if I switch back to the tab that was displaying the wrong controls, it goes back to normal (but still all disabled).

I am completely lost. Does anyone know how this little subroutine could be causing such weird problems?
 
The TabControl is specifically designed to not create controls that are on pages that have not yet been displayed. I would suggest that you write a bit of code an execute it in the Shown event handler of the form to loop through the TabPages and select each one before reselecting the default one.
 
Back
Top