Basic4PPC Tab Control

rbw152

Member
Licensed User
Longtime User
We have a Windows Mobile app that uses the Tab control from the ControlsEx library. As a form loads a new page is added to the Tab control and is populated with different controls such as TextBoxes etc. depending on settings in a database.

This works fine first time but how do I remove these controls when another database is selected?
If I do Tab1.RemoveTabPage it’s blank next time round, even if I add a new tabpage.

If I add another tab and bring it to the front this works fine for the second time but if the user then selects a previously used database an error occurs when loading the tab page because it says the controls have already been added from the previous time.

What I would really like to do is clear the tab of all controls and re-populate it. Or remove the tabs and add them again. Any ideas? Thanks very much.
 

mjcoon

Well-Known Member
Licensed User
I don't have any experience with the Tab control, so I have just been reading the Help pages on it and its methods.

Could you explain how many pages you intend to have in your form? (And thus why you are using them at all.)

For instance you say: "As a form loads a new page is added ..."; does this happen every time? Then you say: "... if the user then selects a previously used database ..."; does this mean you wish to retain a tab page for each previously opened database in one session of program use? And the user selects a database by selecting the tab page?

I note that the Help Overview says: "The controls added would be removed from the form and will be added to the TabControl." So evidently each such control is then not available to be added to another tab page. Nothing is said about what happens to the controls on a tab page if that page is Removed.

BTW Nor is there an explanation of how the indexing of other pages is effected by the Remove-al; presumably pages with a higher index number then have a number one less.

Maybe you could attach some screen-shots of how you have already arranged the appearance of your tab control, as a basis for explaining how you wish them to appear.

Mike
 

rbw152

Member
Licensed User
Longtime User
Hi MJ

thanks for your reply.

What we would like to happen is the user selects a database and opens a details form. This form dynamically loads a tab control and populates it with controls, depending on what is in the database i.e.
Tab1.New1("frmAudit", 0, 28, 239, 218)
For i = 1 To TotalTabs
Tab1.AddTabPage("Page " & i)
Next i
Tab1.BringToFront

For i = 1 To NumFields
AddLabel("frmAudit", LabelName, 5, 10,ControlWidth, LabelHeight, FieldNames(i))
tab1.AddControl(LabelName,TabNumber,0,TopPosition)
Control(LabelName).color = 253,185,16
Next

etc. (not always labels, coule be anything)

Then when the form is closed I would like to 'de-populate' the tab control or better still get rid of altogether so that when another database is selected and the details form is re-opened, a new tab control is created and populated with a different set of controls.

So in answer to your question, yes we would like to get rid of the tab every time the details form is closed, although we would be happy if we could get rid of the controls on it ready for next time.

However, 'Dispose' when closing the form stops us from using the Tab control again next time.

Removing the tabpage seems to work but when you attempt to add a control that was used once before (when a previous database is selected again) it says it's already been added and fails with an error.

I don't understand why when we call RemoveTabPage it doesn't remove the controls that were added to it earlier. Or why there isn't a RemoveControl method. That would be really useful!

It seems that if you add a tab control, then a tabpage and some controls they remain there for the duration of the program, which is not useful.
 

mjcoon

Well-Known Member
Licensed User
I don't understand why when we call RemoveTabPage it doesn't remove the controls that were added to it earlier. Or why there isn't a RemoveControl method. That would be really useful!

It seems that if you add a tab control, then a tabpage and some controls they remain there for the duration of the program, which is not useful.

Maybe tabs were not intended to be as dynamic as you are using.

You do not explain how you choose the value of TotalTabs or of TabNumber. The rest of your description reads as if there is only one tab page, which would be pointless. If the multiple tabs exist merely to provide extra screen area for showing controls (as the Help Overview suggests) then you could (presumably) discard all the pages at once by repeating the
B4X:
Tab1.New1("frmAudit", 0, 28, 239, 218)

Mike (still a bit baffled).
 

Basic4Life

Member
Licensed User
However, 'Dispose' when closing the form stops us from using the Tab control again next time.

Removing the tabpage seems to work but when you attempt to add a control that was used once before (when a previous database is selected again) it says it's already been added and fails with an error.

I don't understand why when we call RemoveTabPage it doesn't remove the controls that were added to it earlier. Or why there isn't a RemoveControl method. That would be really useful!


The problem here are the debugging features in B4P and the use of an external library. So the error is only thrown when you run your program within B4P(it doesn't catch the dispose of the controls that have been added to the tabcontrol pages). There's no error in the compiled program.

You can use the tabcontrol.dispose method without problems, you just have to add
B4X:
AddObject("Tab1","TabControl")
Tab1.New1(...)

before using it again.

However, this does not get rid of the problem when running from within B4P.

I've attached 2 ways how you can handle the problem during development, that should be removed before shipping the final version.

The easy way uses an arraylist to keep track of all controls added to the tab control pages. When you dispose of the tab control you dispose of all controls in that list, so B4P updates its internal table of controls.

The convenient way is a little more dangerous to use, since it directly interacts with the internal table of controls, but you only have to temporarily copy/paste 1 Sub, a single line of code and add the door library in your existing project.
 

Attachments

  • easy_way.sbp
    1.6 KB · Views: 184
  • convenient_way.sbp
    3.9 KB · Views: 211
Last edited:

rbw152

Member
Licensed User
Longtime User
Basic4Life I think you've cracked it!

Thanks very much indeed. I was thinking along similar lines myself at one point but thought it might be a tedious thing to do. However, you have shown me that it is actually quite simple and I am very grateful for that.

What I still don't really like however is that controls added to a tab page are not necessarily associated with that tab page. You can dispose of the whole Tab control itself but labels etc. added to it are still present somewhere. Removing the Tab control is not enough to remove the controls as well.

Your array idea works perfectly though and means I can get on with the rest of the project now. Yay!
 
Top