Search Tools Links Login

Create New Controls At Runtime !


Visual Basic 6, or VB Classic

This code will allow you to create a new instance of a control
at runtime !
Imagine. When your app is RUNNING you can create a new command button
or a textbox on the fly ! The code they didn't want you to know ;)

Original Author: Marc A. Foumberg

API Declarations

NONE

Code

'Create a new project.
'Add a command button.
'Name the button...
' Command1(0)
'As if it were an aray.
'Its sometimes easyier to create
'an aray to begin with. If you do
'be sure to delete all button except
'Command1(0).
'The Code...
Private Sub Command1_Click(Index As Integer)
Static I As Integer
I = I + 1
Load Command1(I)
Command1(I).Left = Command1(I - 1).Left + 200
Command1(I).Top = Command1(I - 1).Top + 600
Command1(I).Caption = "New Button !"
Command1(I).Visible = True
End Sub
'At runtime this will create a new
'command button.
'To add additional function you could add
'an IF statement. As follows...
Private Sub Command1_Click(Index As Integer)
On Error GoTo Handler1
'Create new button
Static I As Integer
I = I + 1
Load Command(I)
Command(I).Left = 2460
Command(I).Top = 5520
Command(I).Caption = "For Real This Time" ' change the caption
Command(I).Visible = True
' Code to unload the form when the new button is clicked
If Command(1) Then
Unload Me
End If
Handler1:
End Sub
'Email Marc at 3dtech@acwn.com with any questions.
'try this with other controls !

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 95 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.