[Gambas-user] How to dynamically add & populate variable number of tabs at runtime?

T Lee Davidson t.lee.davidson at ...626...
Wed Apr 19 17:55:54 CEST 2017


Thank you, Matti. That got me started.

I now have a working test model that will create a variable (unknown at design-time) number of tabs and populate each of them 
with a _visible_ GridView. It works great.

The form now has a ValueBox to input the number of tabs to create, an AddTabs button, a PopulateGrids button, and of course a 
TabStrip. Here's the code for anyone else looking for a basic demo. (Note the use of the Object.Add method.)


[code]
' Gambas class file

Private aGridViews As New Object[]

Public Sub Form_Open()

   Dim hGridView As GridView

   ValueBox1.Value = 3 'convenience

   TabStrip1[0].Text = "Tab 0"
   TabStrip1.Index = 0

   hGridView = New GridView(TabStrip1) As "GridViews"
   aGridViews.Add(hGridView, 0)
   With aGridViews[0]
     .X = 0
     .Y = 0
     .Width = .Parent.Width
     .Height = .Parent.Height
     .Columns.Count = 2
     .Rows.Count = 2
     .Columns.Width = .Width / 2
   End With

End

Public Sub TabStrip1_Close(Index As Integer)

   TabStrip1.Index = Index
   TabStrip1.Current.Children[0].Delete() 'Comment out to test if tab is empty
   TabStrip1.Current.Delete()

End

Public Sub btnAddTabs_Click()
   Dim i As Integer
   Dim hGridView As GridView

   If ValueBox1.Value = 0 Then Return

   TabStrip1.Count += ValueBox1.Value
   For i = 1 To ValueBox1.Value
     TabStrip1[i].Text = "Tab " & i
     TabStrip1.Index = i

     hGridView = New GridView(TabStrip1) As "GridViews"
     aGridViews.Add(hGridView, i)
     With aGridViews[i]
       .X = 0
       .Y = 0
       .Width = .Parent.Width
       .Height = .Parent.Height
       .Columns.Count = 2
       .Rows.Count = 2
       .Columns.Width = .Width / 2
     End With
   Next

End

Public Sub btnPopulateGrids_Click()
   Dim i As Integer

   For i = 0 To aGridViews.Max
     aGridViews[i][0, 0].Text = "Tab " & i & ": [0,0]"
     aGridViews[i][0, 1].Text = "[0,1]"
     aGridViews[i][1, 0].Text = "Tab " & i & ": [1,0]"
     aGridViews[i][1, 1].Text = "[1,1]"
   Next

End
[/code]


On 04/19/2017 01:45 AM, Matti wrote:
> Hi Lee,
>
> first of all, you have to give the GridView a X, Y, width and height. Otherwise it's there but doesn't show.
>
> To reference the GridViews, create an array of them and address them as GridView[0], GridView[1] and so on. The [i] is the number of the TabStrip index.
>
> Here is an example of what I did recently (tsTasks is the TabStrip):
>
> Private aTaskText As New Object[13]
> Private aTaskCheck As New Object[13]
> ...
>    For i = 1 To 12
>      tsTasks[i - 1].Text = aMonths[i]
>      tsTasks.Index = i - 1
>
>      aTaskText[i] = New TextBox(tsTasks) As "Tasks"
>      With aTaskText[i]
>        .X = 77
>        .Y = 21
>        .Width = 728
>        .Height = 35
>      End With
>
>      aTaskCheck[i] = New CheckBox(tsTasks) As "CheckDone"
>      With aTaskCheck[i]
>        .X = 840
>        .Y = 28
>        .Width = 126
>        .Height = 21
>        .Text = ("done")
>        .Tag = i
>      End With
>

-- 
Lee




More information about the User mailing list