B4J Question Add a tab to a tab pane.

Basy

New Member
How do I add a tab to a tab pane where the tab pane already exists on the visual designer? The examples I've looked at aren't helping me and I'm still confused.

Thank you.
 

DonManfred

Expert
Licensed User
Longtime User
 
Upvote 0

MegatenFreak

Active Member
Licensed User
To put it in a very brief example, first create a separate layout in the designer and put in it the contents you want to show in the new tab.
Let's say you've named that layout file "new_tab_contents"
Then:

B4X:
Public MyTabPane As TabPane     'Existing in the designer

Dim new_tab As TabPage             'For the tab you want to add
new_tab = MyTabPane.LoadLayout("new_tab_contents", "My New Tab Header")         'The second argument shows as the header for your tab

Just like that!
 
Upvote 0

Basy

New Member
DonManfred, thanks for the suggestion, I saw that thread before and I tried applying it but I kept getting errors and I couldn't figure it out.

MegatenFreak, thanks for the code. Here's what I did...

01 - I start B4J.
02 - Code Editor > File > New > UI.
03 - Project Name = Test > OK.
04 - Code Editor > Designer > Open Internal Designer.

Then I am shown 2 things...

A layout in the visual designer.
A form.

05 - I close the form.
06 - Visual Designer > Menu > Add View > TabPane.

This tabpane is called "TabPane1".

07 - I change the name to "MyTabPane".
08 - In the code editor I add your code to the button code...

B4X:
Sub Button1_Click
    xui.MsgboxAsync("Hello World!", "B4X")
    Public MyTabPane As TabPane
    Dim new_tab As TabPage
    new_tab = MyTabPane.LoadLayout (new_tab_contents", "My New Tab Header")
End Sub

09 - Then I see..

Main - 23: Syntax error.
Main - 21: Variable 'MyTabPane' was not initialized. (warning #11)
Main - 22: Unused variable 'new_tab'. (warning #9)

10 - So I try initializing MyTabPane using the some code from a previous forum post (ie: "TP.Initialize") thinking that this would not only initialize MyTabPane but it might also fix the syntax error.

B4X:
Sub Button1_Click
    xui.MsgboxAsync("Hello World!", "B4X")
    Public MyTabPane As TabPane
    MyTabPane.Initialize
    Dim new_tab As TabPage
    new_tab = MyTabPane.LoadLayout (new_tab_contents", "My New Tab Header")
End Sub

11 - Then I see...

Main - 22: '(' expected.
Main - 24: Syntax error.
Main - 23: Unused variable 'new_tab'. (warning #9)

12 - Then I put the mouse over the problematic code and I see...

'(' expected.
Initialize (EventName As String)

13 - I don't know what the event name is, so I look at the previous forum post I mentioned before, and then I try this code...

B4X:
MyTabPane.Initialize (add)

14 - Then I see...

Main - 22: Undeclared variable 'add' is used before it was assigned any value.
Main - 24: Syntax error.
Main - 23: Unused variable 'new_tab'. (warning #9)

Any thoughts?
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
It looks like the problem is that you cannot create a tabpane directly as it is a UI element.

You need to load a layout with a TabPane in it.

B4X:
Sub Button1_Click
    xui.MsgboxAsync("Hello World!", "B4X")
    ' The following 2 lines will not work. Cannot create a tabpane directly as it is a Custom Control
    Public MyTabPane As TabPane
    MyTabPane.Initialize
    Dim new_tab As TabPage
    new_tab = MyTabPane.LoadLayout (new_tab_contents", "My New Tab Header")
End Sub

You need to use the method shown here:

so:
(Contents of B4xMainPage)
B4X:
Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Label1 As B4XView
    
    Private TabStrip1 As TabStrip
    Private Button1 As B4XView
End Sub

Public Sub Initialize
    
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
 '   xui.MsgboxAsync("Hello world!", "B4X")
    Private pnl As B4XView = xui.CreatePanel("")
    
    Root.AddView(pnl,0,Button1.Height+Button1.Top+10dip,Root.Width,Root.Height-(Button1.Top+Button1.Height+10dip))
    pnl.LoadLayout("TabHolder")
    
    TabStrip1.LoadLayout("new_tab_contents","My New Tab Header")
End Sub

I assume that this is a sample program to understand how to create a tab strip programmatically. as each click on the button will create a whole new tab strip.
 

Attachments

  • Project.zip
    16.3 KB · Views: 127
Upvote 0
Top