Dear visitor, welcome to Jabaco - Community.
If this is your first visit here, please read the Help. It explains in detail how this page works.
To use all features of this page, you should consider registering.
Please use the registration form, to register here or read more information about the registration process.
If you are already registered, please login here.
Creating dinamically controls on a form
Hi everyone,
I need to know how to create dinamically controls on a form. In vb6 it's possible to do it using control-arrays creating the first control with index = 0 and then creating the others by code using Load .
Example:
dim i as integer
For i = 1 To 5
Load Label1(i)
Label1(i).Top = Label1(i - 1).Top + Label1(i - 1).Height
Label1(i).Left = Label1(i - 1).Left
Label1(i).Visible = True
Next
Thanks.
Here's an example:
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
Public Sub Form_Load()
Const INITIAL_X = 10
Const INITIAL_Y = 10
Const BUTTON_WIDTH = 100
Const BUTTON_HEIGHT = 30
Dim i As Integer
Dim buttons(5) As CommandButton
For i = 0 To 5
buttons(i) = New CommandButton()
buttons(i).Caption = "button " & i
buttons(i).Top = INITIAL_Y + (BUTTON_HEIGHT * i)
buttons(i).Left = INITIAL_X
buttons(i).Width = BUTTON_WIDTH
buttons(i).Height = BUTTON_HEIGHT
Me.add(buttons(i))
Next
End Sub
|
I hope it will be useful for
Thanks a lot FrancoAA!
Hi FrancoAA,
i've tried your solution and it works but in this way is not possible to mange the events of the created controls.
There is not the possibilty to create a new control of a control-arry and so to use the event handler of the control-array (es: button_Click(Index As Integer) )?
Thanks
Look that
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
Option Explicit
Implements java#awt#Event#ActionListener
Public Sub Form_Load()
Const INITIAL_X = 10
Const INITIAL_Y = 10
Const BUTTON_WIDTH = 100
Const BUTTON_HEIGHT = 30
Dim i As Integer
Dim buttons(5) As CommandButton
For i = 0 To 5
buttons(i) = New CommandButton()
buttons(i).Caption = "button " & i
buttons(i).Top = INITIAL_Y + (BUTTON_HEIGHT * i)
buttons(i).Left = INITIAL_X
buttons(i).Width = BUTTON_WIDTH
buttons(i).Height = BUTTON_HEIGHT
buttons(i).Parent.addActionListener(Me)
Me.add(buttons(i))
Next
End Sub
Public Sub actionPerformed(arg2 As ActionEvent)
Dim btn As CommandButton
btn = Cast(arg2.getSource(), CommandButton)
MsgBox(btn.Caption)
End Sub
|
That's it
Thanks for this code.
One question: How can I identify which dynamically created instance of the command button is being clicked from within Public Sub actionPerformed(arg2 As ActionEvent)?
I'd like to be able to reference the index (i) of the control when the actionPerformed method is invoked.
I'm actually trying to do something with a label, not a CommandButton but without knowing a more elegant way of referencing which particular control is being clicked within the control array, the only workaround I could really think of would be to do something like this:
|
Jabaco Source
|
1
|
buttons(i).tag=i
|
within
Form_Load()
and then:
|
Jabaco Source
|
1
|
buttons(cint(btn.tag)).backcolor=vbRed
|
in
Public Sub actionPerformed(arg2 As ActionEvent)