How to dynamically add & populate variable number of tabs at runtime?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to dynamically add & populate variable number of tabs at runtime?

T Lee Davidson
Hello Folks,

I have been trying to figure out how to, at runtime, create a variable number of new tabs in a TabStrip, populate those tabs
with Controls, and then be able to reference those controls, all dynamically.

There seems to be little information available as to how to 1) dynamically create controls, and 2) reference dynamically named
controls after creation.

Even with just a simple experiment, I cannot get a GridView to display in a new tab. The Form has been given, at design-time, a
TabStrip and two buttons. The tabs are 'closable'. One button, 'btnAddTab', creates a new tab and a GridView with the new,
current tab as parent. (At least that's what I think it is doing.) The other button, 'btnTabInfo', displays info about the
currently selected tab.

And, I am assuming the GridViews need to have unique names.

[code]
' Gambas class file


Public Sub btnAddTab_Click()
   Dim hGridView As GridView

   TabStrip1.Count += 1
   TabStrip1.Text = "Tab " & TabStrip1.Index
   hGridView = New GridView(TabStrip1) As "GridView" & TabStrip1.Index
   hGridView.Show
   TabStrip1.Refresh

End

Public Sub btnTabInfo_Click()

   Print TabStrip1.Children[0].Name
   Print TabStrip1.Children[0].Enabled
   Print TabStrip1.Children[0].Visible

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
[/code]


Tab creation works, but the GridView control does not display. Any attempt to close a tab without first deleting its children
causes an exception, showing that the tab is indeed being populated with a child control. Clicking on btnTabInfo for "Tab 1" gives:
GridView1
True
True

So, the program is doing at least some of what I've told it to. I'm obviously not telling it the right thing.

How does one:
1. Create new controls and make them visible?
2. Reference new controls after creation [ie. (de-)reference a string as a the name of a control]?


--
Lee


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: How to dynamically add & populate variable number of tabs at runtime?

Matti
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


Am 19.04.2017 um 01:58 schrieb T Lee Davidson:

> Hello Folks,
>
> I have been trying to figure out how to, at runtime, create a variable number of new tabs in a TabStrip, populate those tabs
> with Controls, and then be able to reference those controls, all dynamically.
>
> There seems to be little information available as to how to 1) dynamically create controls, and 2) reference dynamically named
> controls after creation.
>
> Even with just a simple experiment, I cannot get a GridView to display in a new tab. The Form has been given, at design-time, a
> TabStrip and two buttons. The tabs are 'closable'. One button, 'btnAddTab', creates a new tab and a GridView with the new,
> current tab as parent. (At least that's what I think it is doing.) The other button, 'btnTabInfo', displays info about the
> currently selected tab.
>
> And, I am assuming the GridViews need to have unique names.
>
> [code]
> ' Gambas class file
>
>
> Public Sub btnAddTab_Click()
>     Dim hGridView As GridView
>
>     TabStrip1.Count += 1
>     TabStrip1.Text = "Tab " & TabStrip1.Index
>     hGridView = New GridView(TabStrip1) As "GridView" & TabStrip1.Index
>     hGridView.Show
>     TabStrip1.Refresh
>
> End
>
> Public Sub btnTabInfo_Click()
>
>     Print TabStrip1.Children[0].Name
>     Print TabStrip1.Children[0].Enabled
>     Print TabStrip1.Children[0].Visible
>
> 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
> [/code]
>
>
> Tab creation works, but the GridView control does not display. Any attempt to close a tab without first deleting its children
> causes an exception, showing that the tab is indeed being populated with a child control. Clicking on btnTabInfo for "Tab 1" gives:
> GridView1
> True
> True
>
> So, the program is doing at least some of what I've told it to. I'm obviously not telling it the right thing.
>
> How does one:
> 1. Create new controls and make them visible?
> 2. Reference new controls after creation [ie. (de-)reference a string as a the name of a control]?
>
>

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: How to dynamically add & populate variable number of tabs at runtime?

T Lee Davidson
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

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user