You are not logged in.

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.

Vicamau

Beginner

  • "Vicamau" started this thread

Posts: 10

Date of registration: Jul 28th 2010

  • Send private message

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

  • "FrancoAA" is male

Posts: 15

Date of registration: May 4th 2010

Location: Rosario, Argentina

  • Send private message

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

Vicamau

Beginner

  • "Vicamau" started this thread

Posts: 10

Date of registration: Jul 28th 2010

  • Send private message

3

Thursday, July 29th 2010, 11:53am

Thanks a lot FrancoAA! :)

Vicamau

Beginner

  • "Vicamau" started this thread

Posts: 10

Date of registration: Jul 28th 2010

  • Send private message

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

  • "FrancoAA" is male

Posts: 15

Date of registration: May 4th 2010

Location: Rosario, Argentina

  • Send private message

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

vpr

Beginner

  • "vpr" is male

Posts: 30

Date of registration: May 21st 2014

  • Send private message

6

Sunday, November 2nd 2014, 7:34pm

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.

vpr

Beginner

  • "vpr" is male

Posts: 30

Date of registration: May 21st 2014

  • Send private message

7

Wednesday, November 5th 2014, 12:20am

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)

Rate this thread
WoltLab Burning Board