Click here to Skip to main content
15,746,108 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Does anyone know how to create object (like textbox, command button, label, and so on) on runtime with VB6?
I means that when I clicked one button or do any action, then new textbox will appear.

In VB.Net look like this
Dim txt as New Textbox

But in VB6 don't know what's "New"..

Many Thanks

update : I intended to create new object from scratch, so we won't use any object that had been create then. <<--- If U know what I means, it's Control Array!
Updated 3-Mar-20 21:12pm
Sergey Alexandrovich Kryukov 25-Jun-12 21:11pm    
Is there any single reason to deal with this garbage called "VB6" at all? And even ask questions about it?
Himachandra 29-Jun-12 3:43am    
No sense at all.....
Y you are giving unreasonable suggestions....
If you know ans regarding ques...... then give it other wise
Sergey Alexandrovich Kryukov 29-Jun-12 18:00pm    
Well, this is you who think this suggestion is unreasonable (even though it was just a question, suggestive, perhaps). I'm sure VB6 makes no sense at all. Do you see any single reason to use it? :-)
satrio_budidharmawan 25-Jun-12 21:14pm    
I don't know why, but there're many student still VB6-ing on their Final Project, that's too shame..

Create a Control dynamically at Run time. Without using a Control Array. That is the control is not present at Design time But will appear at Run Time.

option Explicit
Dim withevents Cmd1 as CommandButton
private Sub Form_Load()
set Cmd1 = Controls.Add("vb.commandbutton", "Cmd1")
Cmd1.Width = 2000
Cmd1.Top = me.Height / 2 - Cmd1.Height / 2 - 100
Cmd1.Left = me.Width / 2 - Cmd1.Width / 2 - 100
Cmd1.Caption = "Dynamic Button"
Cmd1.Visible = true
End Sub
private Sub Cmd1_click()
MsgBox "I have been Created Dynamically at Run-time", _
, "Dynamic Controls"
End Sub
Share this answer
satrio_budidharmawan 26-Jun-12 2:15am    
how if I need to create a lot of textbox (say it 100 of textbox)?
Do I need to
Dim cmd1 to cmd100?
sng0006 27-Jun-20 9:07am    
Very Useful, Thank you.
How to make an array of labels using this code? I kept changing elements in the code to make it create an array of labels (with captions from 1 to 100 to each label). but no avail.
Why don't you create the Textbox and set it wherever you want and set it's visibility property to false, then based on whatever you want say another button click that you set it to visible. Just a thought!!!!
Share this answer
satrio_budidharmawan 2-Jul-12 21:16pm    
Nope, you forget the terms "Dynamically"
Dim intAdd As Integer
intAdd = InputBox("key in how many number of textboxes to be added")
For x = 1 To intAdd
Load txtAdd(x)
txtAdd(Index).Top = txtAdd(Index).Top + 600
txtAdd(x).Visible = True
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900