TabControl won't dispose?

willisgt

Active Member
Licensed User
Okay, now, don't do this to me.

I've got a form, onto which I place a TabControl; on the various tab pages, I place panels, labels, etc. Everything is created dynamically. When each control is created, I add the name of that control to an ArrayList on the form.

When the form closes, I read through the ArrayList, from bottom to top, and dispose of each control.

This all works very will *until* I get to the TabControl. Running within the IDE doesn't produce any errors, but compile (optimized) for the device, and the Control( name ).Dispose produced an Invalid Cast Exception error.

The error occurs when I try to dispose of the TabControl.

I also started checking the type of the control before disposal, and using Control( name, TabControl ).Dispose. Same problem.

I imagine that, instead of disposing of the tab control, I could just remove all of the tab pages and set the visible to false; but I'd rather dispose of it.

Anyone?


Gary

:sign0085:
 

agraham

Expert
Licensed User
Longtime User
I don't see this problem I'm afraid. What error occurs when you use the qualified form of Control()?

ctrl = "tab"
Control("ctrl).Dispose
'Gets compiled buts throws a runtime Invalid Cast error


ctrl = "tab"
Control(ctrl, TabControl).Dispose
'Works fine at runtime

I also started checking the type of the control before disposal, and using Control( name, TabControl ).Dispose. Same problem.
If you are checking using ControlType then look at what it returns with a msgbox, it is not what you may think. You need to check for "TabControl.TabControl" not just "TabControl"
 

willisgt

Active Member
Licensed User
I am using ControlsEX.dll version 1.0.2995.34468, created 10 March 2008 at 7:52 AM.

This is the actual code I'm using:

B4X:
Sub formGeneric_Close

   c = arrayListGenericControls.Count
   
   For x = ( c - 1 ) To 0 Step -1
   
      cn = arrayListGenericControls.Item( x )
   
      If StrIndexOf( ControlType( cn ), "TabControl", 0 ) <> -1 Then

         Control( cn, TabControl ).Dispose

      Else
   
         Control( cn ).Dispose

      End If

      arrayListGenericControls.RemoveAt( x )

   Next

End Sub

(One of these days I'm going to learn to include my code in the first post of a thread.)

:sign0161:
 
Last edited:

agraham

Expert
Licensed User
Longtime User
I think that you have missed the significance of this sentence I posted.
If you are checking using ControlType then look at what it returns with a msgbox, it is not what you may think. You need to check for "TabControl.TabControl" not just "TabControl"

EDIT :- Actually I'm wrong. I can't immediately see why that doesn't work.
 
Last edited:

agraham

Expert
Licensed User
Longtime User
I slightly simplified your code for testing and it works for me. Perhaps you have something else wrong - like a case-difference with the names in the ArrayList.
B4X:
Sub Form2_Close
      cn = "tab"   ' tab is the name of the TabControl
      If StrIndexOf( ControlType( cn ), "TabControl", 0 ) <> -1 Then
         Control( cn, TabControl ).Dispose
      Else   
         Control( cn ).Dispose
      End If
End Sub
 

willisgt

Active Member
Licensed User
Andy, you get full credit for this fix...

Apparently, your suggestion of checking both type 'TabControl' and 'TabControl.TabControl' had actually fixed the problem. Thing is, I also have a scrollbar on this form. So when I tried to dispose of the scrollbar, I was producing yet another cast exception error - identical, but from a different source.

My apologies for the oversight - I should have realized that both TabControl and ScrollBar types would do this.

:sign0161:
 
Top