is there a tuturial for dynamic control creation

Ask about the individual Gambas components here.
sadams54
Posts: 83
Joined: Monday 09th July 2018 3:43am

is there a tuturial for dynamic control creation

Post by sadams54 » Wednesday 25th August 2021 9:47pm

I am totally new to dynamically creating controls. what I have to do is create about a dozen controls and events for controls in a tab panel. I also have to create multiple tabs in that panel each with about a dozen controls and event handlers. I could use a good tuturial on how to do this.

User avatar
BruceSteers
Posts: 657
Joined: Thursday 23rd July 2020 5:20pm

Re: is there a tuturial for dynamic control creation

Post by BruceSteers » Wednesday 25th August 2021 11:33pm

I'm not exactly sure what you need exactly when you say "dynamic control creation" but maybe this helps.

If you mean adding controls to your form with code then that's not too hard.

consider this code...

Private $hButton As Button

Public Sub Form_Open()

$hButton = New Button(MyButtonPanel) As "MyButton"
$hButton.Text = "New Button"
$hButton.AutoResize = True

End

Public Sub MyButton_Click()

  Print "Button got clicked"

End

Or this code...

Public Sub Form_Open()

  Dim sNames As String[] = ["Fred", "Sally", "Bob"]
  Dim $hButton As Button

  For Each s As String In sNames

    With $hButton = New Button(MyButtonPanel) As "MyButton"
      .Text = s
      .AutoResize = True
      .Name = s
    End With

  Next

End

Public Sub MyButton_Click()

  Print "button clicked was";; Last.Name 

End

Notice in the last example i used With / End With to save typing the button name all the time.

That will create 3 buttons that all use the same event handler MyButton_Click()
you can use Last.Name to get the name of the button

That should get you started :)
I used a couple of different methods there.
the 1st example uses a global $hButton variable, the other does not.
A lot of times you do not need a global variable as you can get the button using the Last keyword in the handler.
Wishing well
Bruce

If at first you don't succeed , try it differently.

sadams54
Posts: 83
Joined: Monday 09th July 2018 3:43am

Re: is there a tuturial for dynamic control creation

Post by sadams54 » Thursday 26th August 2021 12:09am

I managed to get that far but the entire scope of what I am thinking may be beyond gambas ability. I am going to explore control arrays to do this.
I have a tab panel that I was going to dynamically create additional tabs as needed each one needs about 2 dozen controls in it. labels, textboxes, buttons, spinbuttons, and comboboxes for each panel and there can be a dozen or 2 tabs. Problem is once I create something the next one throws an error that it is already created. Then the butons fill the container which is a problem also. I think I will abandon the dynamic creation and try a finite number of tabs and make them visible as needed and try control arrays for the controls. Something else I will have to learn about.

thank you for the response tho.

User avatar
Quincunxian
Posts: 134
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: is there a tuturial for dynamic control creation

Post by Quincunxian » Thursday 26th August 2021 7:29am

Have a look at the webview example in Gambas examples on the site.
From memory, it creates tabs automatically when you select the correct option.
Might give you a nudge in the right direction.
Cheers - Quin.
I code therefore I am

User avatar
BruceSteers
Posts: 657
Joined: Thursday 23rd July 2020 5:20pm

Re: is there a tuturial for dynamic control creation

Post by BruceSteers » Thursday 26th August 2021 8:58am

sadams54 wrote:
Thursday 26th August 2021 12:09am
I managed to get that far but the entire scope of what I am thinking may be beyond gambas ability. I am going to explore control arrays to do this.
I have a tab panel that I was going to dynamically create additional tabs as needed each one needs about 2 dozen controls in it. labels, textboxes, buttons, spinbuttons, and comboboxes for each panel and there can be a dozen or 2 tabs. Problem is once I create something the next one throws an error that it is already created. Then the butons fill the container which is a problem also. I think I will abandon the dynamic creation and try a finite number of tabs and make them visible as needed and try control arrays for the controls. Something else I will have to learn about.

thank you for the response tho.
This is most certainly not beyond Gambas's ability.
It is just currently beyond yours ;)

(I do not mean that rudely, i Mean hold on because you're about to level up ;) )

You should be able to create a Form
Better still i will quickly do it and show you..
See attached project.

What i did was make a form called TabControls

then i add the TabControls Form to my TabPanel1.

Dim hContents As TabControls = New TabControls(TabPanel1) As "TPanel"

As It is is a Form "TPanel" will have all the Form Events available like TPanel_MousUp(), etc plus any we add.

On pressing the "New Tab" button it dynamically adds another tab panel and adds a new TabControls form to it.
Tabs can also be removed. (i added a little handling to cancel closing a tab)

I added an event called TabClosed that triggers TPanel_TabClosed() when the close button is pressed just as an example of how to add an Event trigger.

I also added an extra Tag field to the form called MyData as an example of how to add properties/variables to each form created.

Study the code slowly.
Note the use of the Last keyword being to used to access the tab panels contents without needing global variables to them.

Last is very handy , Last.Parent can also be a handy thing to know

you can add any number of functions to the TabControls Form_Open() to dynamically add or configure current controls as it loads into a Tab.
(to access controls that are in TabControls form from the main class you will need to set each controls Public = True)
Attachments
_aa.zip
(20.77 KiB) Downloaded 33 times
Last edited by BruceSteers on Thursday 26th August 2021 10:14am, edited 9 times in total.
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
BruceSteers
Posts: 657
Joined: Thursday 23rd July 2020 5:20pm

Re: is there a tuturial for dynamic control creation

Post by BruceSteers » Thursday 26th August 2021 9:04am

PS. it's hard to tell what you are doing wrong if you do not post your code.
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
BruceSteers
Posts: 657
Joined: Thursday 23rd July 2020 5:20pm

Re: is there a tuturial for dynamic control creation

Post by BruceSteers » Thursday 26th August 2021 10:05am

PS. you must experiment with things like HBox, VBox, Panel.Arrangement = Arrange.Vertical or Arrange.None.

experiment, experiment, then experiment some more :)

You are having problems with buttons expanding.

consider making a panel with Panel1.Arrangement = Arrange.Vertical ' (objects arrange downwards)
Then add a HBox and DO NOT set it to expand (a HBox will automatically expand horizontally not vertically in a vertically arranged parent)
Inside the HBox place your buttons / etc

if your form has a TextArea or ListBox/GridView type of control make that expandable so it fills the rest of the form.

the HBox should keep it's height.
If you do not want the buttons to expand horizontally add a Spring to one side of them to scrunch them up.

In the above example i have done this
Hope this helps
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
cogier
Site Admin
Posts: 764
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: is there a tuturial for dynamic control creation

Post by cogier » Thursday 26th August 2021 1:14pm

Put all the controls you want on one tab. Post the form so we can see it. We might be able to create the necessary code for you. It is very difficult to help when we don't know what the end game is.

Have a look at my 15 Puzzle game on the Gambas Farm. All the GUI components are created in code.

sadams54
Posts: 83
Joined: Monday 09th July 2018 3:43am

Re: is there a tuturial for dynamic control creation

Post by sadams54 » Thursday 26th August 2021 8:06pm

you guys are great. I will take a look at the examples you pointed out. No worry I did not take it as an insult about above my ability. If I get deparate i will post the form so you can help with code but I want to learn how to do it as I am not in a rush for this project. We learn things with each new project and I want to learn these skills. I had them down in VB quite well so I understand the concepts just now to learn the mechanics.

sadams54
Posts: 83
Joined: Monday 09th July 2018 3:43am

Re: is there a tuturial for dynamic control creation

Post by sadams54 » Thursday 26th August 2021 11:31pm

I got the dynamic creation of everything figured out and working. Thank you guys very much. Thank you both for the examples they helped.

My next 2 issues are hopefully simple.

How do I create a control array? This is rather important for this project.

the 2nd is in the event handler. following cogier's example I have the following code and it worked at first then stopped working so wondering what I am doing wrong. when I click the button nothing happens.

Public Sub Testbutton_Click()

message(Last.Name)

End

Public Sub MakeChangeCamNameButton(cTab As Integer)

Dim P As New Panel(tCam[cTab])
P.Name = "p" & Str(cTab)
P.AutoResize = False

Dim B1 As New Button(P) As "Testbutton"
B1.Width = 160
B1.Height = 32
B1.X = 568
B1.Y = 16
B1.Name = "btnChangeCamName" & Str(cTab)
B1.Text = "Change Cam Name"

End

Post Reply