Saturday, September 11th 2010, 12:54am UTC+2

You are not logged in.

  • Login
  • Register

1

Wednesday, July 28th 2010, 5:34pm

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.

FrancoAA

Beginner

Posts: 15

Location: Rosario, Argentina

2

Wednesday, July 28th 2010, 6:08pm

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 ;)

3

Thursday, July 29th 2010, 11:53am

Thanks a lot FrancoAA! :)

4

Thursday, July 29th 2010, 12:05pm

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

FrancoAA

Beginner

Posts: 15

Location: Rosario, Argentina

5

Thursday, July 29th 2010, 7:05pm

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 ;)

Rate this thread
WoltLab Burning Board