is there a tuturial for dynamic control creation
Posted
#1
(In Topic #730)
Enthusiast

Posted
Guru

If you mean adding controls to your form with code then that's not too hard.
consider this code…
Or this code…
Code (gambas)
- .Text = s
- .Name = s
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.
Posted
Enthusiast

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.
Posted
Expert

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
I code therefore I am
Posted
Guru

sadams54 said
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)
Posted
Guru

Posted
Guru

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
Posted
Guru

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

Posted
Enthusiast

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
Posted
Guru

sadams54 said
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
does it make a difference if you use
Dim P As Panel = New Panel(tCam[cTab])
and
Dim B1 As Button = New Button(P) As "Testbutton"
An array of controls can be made like this..
Dim MyControls as New Control[]
Mycontrols.Add(Button1)
etc, etc
Or
Dim MyContorls As New Object[]
Posted
Enthusiast

can we create a control array from the gambas IDE?
1 guest and 0 members have just viewed this.


