Disposing Controls

RandomCoder

Well-Known Member
Licensed User
Longtime User
It was my understanding that if a Panel or Form was disposed then all controls belonging to that Panel/Form would also be disposed.
However my latest app has shown that this is not the case.
Is this a change with V6 or am I just mistaken, as it appears to be undocumented?

I'm now using the GetControls method to dispose of all controls on a panel prior to disposing the panel which is working, however I also have a few objects which are not disposed using this method.
Is there a way to determine what objects are present and then can I dispose them?

Thanks,
RandomCoder
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Forms can't be disposed.
When you dispose a panel only the panel gets disposed.
You can use something like:
B4X:
somearray() = GetControls("Panel1")
for i = 0 to ArrayLen(somearray())-1
 Control(somearray(i)).Dispose
next
This is not so elegant but it will work (to find if a specific control exists):
B4X:
Sub ControlExist(crl)
ErrorLabel(ControlExistErr)
a = ControlType(crl)
return true
ControlExistErr:
return false
End Sub
 

willisgt

Active Member
Licensed User
I have often used an ArrayList to store the names of all the controls I create dynamically. This works well if I just want to know if a control exists.

More recently, I've switched to using a table, as I can also store the name of the control's parent (form, panel, etc), type, and other stuff I might want to keep track of.

Naturally, it's a simple matter to add an item to an array list - or a row to a table - when a control is created, and to remove it when a control is disposed of.

Gary
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Forms can't be disposed.

Thanks Erel.
Although the help file states that Forms do indeed have a Dispose property.

I'm currently using a similar code to yours to dispose of the controls on a panel but what about Objects?
Can I use a method to determine what Objects are present on the panel and then dispose them?
Ideally I would like to create and dispose of the tree view, context menu's and SMS messages as and when required, although if not possible then I will add at the start of the program and leave in place.

Regards,
RandomCoder
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
From the lack of responses, do I take it that it's not possible to determine what objects are in use (other than keeping my own record in an array list) and that I can't dispose of them anyway?

Regards,
RandomCoder
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
I will double check the problem I was having and report back later (about to leave for work now :sign0148:).
Thanks for the reply.

Regards,
RandomCoder
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
I found my problem as demonstarted in my sample code attached (just add ControlsEx.Dll to the same dir)...

It appears that Objects do not belong to the panel/form they are applied to, niether do they belong to the main form.

Now that I know this, disposing them is not a problem, I'll just keep track of which ones I need to delete.
My original intention was just to pass a sub the panel name and have it automatically delete all controls associated with it, I'll just need to re-work this slightly.

Thanks for the assistance.

Regards,
RandomCoder
 

Attachments

  • ControlsExample.sbp
    898 bytes · Views: 156
Top