is there a tuturial for dynamic control creation

Post

Posted
Rating:
#1 (In Topic #730)
Avatar
Enthusiast
sadams54 is in the usergroup ‘Enthusiast’
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.
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
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…

Code (gambas)

  1.  
  2. Private $hButton As Button
  3.  
  4. Public Sub Form_Open()
  5.  
  6. $hButton = New Button(MyButtonPanel) As "MyButton"
  7. $hButton.Text = "New Button"
  8. $hButton.AutoResize = True
  9.  
  10.  
  11. Public Sub MyButton_Click()
  12.  
  13.   Print "Button got clicked"
  14.  
  15.  
  16.  

Or this code…

Code (gambas)

  1.  
  2. Public Sub Form_Open()
  3.  
  4.   Dim sNames As String[] = ["Fred", "Sally", "Bob"]
  5.   Dim $hButton As Button
  6.  
  7.   For Each s As String In sNames
  8.  
  9.     With $hButton = New Button(MyButtonPanel) As "MyButton"
  10.       .Text = s
  11.       .AutoResize = True
  12.       .Name = s
  13.     End With
  14.  
  15.   Next
  16.  
  17.  
  18. Public Sub MyButton_Click()
  19.  
  20.   Print "button clicked was";; Last.Name
  21.  
  22.  
  23.  

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.
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Enthusiast
sadams54 is in the usergroup ‘Enthusiast’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
 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
Online now: Yes Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘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)
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
 PS. it's hard to tell what you are doing wrong if you do not post your code.
Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’
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
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Enthusiast
sadams54 is in the usergroup ‘Enthusiast’
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.
Online now: No Back to the top

Post

Posted
Rating:
#10
Avatar
Enthusiast
sadams54 is in the usergroup ‘Enthusiast’
 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
Online now: No Back to the top

Post

Posted
Rating:
#11
Guru
BruceSteers is in the usergroup ‘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[]
Online now: No Back to the top

Post

Posted
Rating:
#12
Avatar
Enthusiast
sadams54 is in the usergroup ‘Enthusiast’
 I tried both ways thinking just like you but made no difference. It seemed to work at first then stopped working so very lost as to why. It does not even fire the event.

can we create a control array from the gambas IDE?
Online now: No Back to the top
1 guest and 0 members have just viewed this.