Create Controls Dynamically (VB6)
Posted
by Manoj Tuli
on February 3rd, 2004
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
'

Comments
Adding Dynamic TextBox with Array Control & Events
Posted by Carlos Fortin on 05/01/2013 07:45amLegacy, just want to let you know that your answer Posted on 09/11/2003 12:00am After all this time is helping me so very much. Thank you,
Replydynamical objects
Posted by Albert on 04/26/2012 01:16amgd morning hve a question regaring dynamically creating of controls using such as 'Set FM1 = Controls.Add("vb.Frame", "Frame1")' posible to use a dynamic name (string) instead of a fixed name? strObjName = "NewObj" & Counter Form1.Controls.Add("vb.textbox", strObjName, Form1) cannot use above system.
Replyhelp me urgently
Posted by raviraj on 04/27/2004 09:48amhow to create form dynamically
Replyhow to create menu control in VB?
Posted by binhthanh on 04/16/2004 02:16amits really a great stuff how run time button generation
Posted by Legacy on 12/24/2003 12:00amOriginally posted by: Pranav Jariwalla
-
ReplyRe to Pravan Jariwalla: Add Dynamic Button to the frame control.
Posted by manoj_tuli on 05/11/2004 03:10amFriend, Heres the code to do the same. Sorry for the delay in Replying. ------------------------------------------ Option Explicit Dim WithEvents Cmd1 As CommandButton Dim WithEvents FM1 As Frame Private Sub Form_Load() Set FM1 = Controls.Add("vb.Frame", "Frame1") FM1.Top = 150 FM1.Left = 300 FM1.Width = 3000 FM1.Height = 2000 FM1.Caption = "Dynamic Frame" 'FM1.Name = "Frame1" FM1.Visible = True Me.Show DoEvents 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" Set Cmd1.Container = FM1 Cmd1.Visible = True End Sub ' Private Sub Cmd1_click() MsgBox "I have been Created Dynamically at Run-time, with Frame 1 as my container.", , "Dynamic Controls" End Sub ------------------------------------------
ReplyAdding event in dynamic control
Posted by Legacy on 12/18/2003 12:00amOriginally posted by: Deen Bandhu
How to add event on dynamically created control.
I have tried with this code.
For i = 1 To 2
ReplySet ctlOpt(i) = Form1.Controls.Add("VB.OptionButton", "Opt" & i, ctlFrm(1))
With ctlOpt(i)
If i = 1 Then
.Top = 100
Else
.Top = ctlOpt(i - 1).Top + 500
End If
.Left = 600
.Caption = .Name
.Visible = True
End With
Next
Now I need fire event on each option button. It is possible with one control but not with array control
Dynamic Control Array
Posted by Legacy on 12/08/2003 12:00amOriginally posted by: Deen Bandhu
ReplyHelp me urgently
Posted by Legacy on 10/28/2003 12:00amOriginally posted by: arun prakash
The code was nice & simple.But I want to know how to write events.I have create an array of textboxes.PLzzzzzzzz help me .Thanks in advance
-
-
ReplyRe: Help me urgently
Posted by manoj_tuli on 05/11/2004 03:25amPlease send me your code on thsi address ping_manoj@hotmail.com. Let me see what exaclt are w etalking about.
ReplyHelp me urgently
Posted by raviraj on 04/26/2004 09:53amThe code was nice & simple.But I want to know how to write events.I have create an array of textboxes.PLzzzzzzzz help me .Thanks in advance
ReplyAdding Dynamic TextBox with Array Control & Events
Posted by Legacy on 09/11/2003 12:00amOriginally posted by: aditul
On a blank form
-create textBox
-rename it to arrayText
-selct the textBox
-copy
-paste
You'll be asked if you want to create a control array 'cause you have another textBox with the same name
-click ok
you have now the array control created.
you can delete the second now.
you'll have one control array with only one control
-fill in this code in Form_Load()
For i = 1 To 10
Load arrayText(i)
arrayText(i).Left = arrayText(0).Left
arrayText(i).Top = arrayText(0).Top + 300 * i
arrayText(i).Text = "array textbox dynamic (" & i & ")"
arrayText(i).Width = 3500
arrayText(i).Height = 285
arrayText(i).Visible = True
MsgBox "textbox dynamic nr " & i, vbOKOnly + vbInformation, "Created???"
Next i
Run it, should work.
-
-
ReplyAdding Dynamic TextBox with Array Control & Events
Posted by Carlos Fortin on 05/01/2013 07:47amHello Legacy, I just want you to know that your answer to this questions after all this time is still helping people. It is helping me a lot. Thank you very much!
ReplyVB Dynamic Controls
Posted by Peter on 04/23/2012 04:15amI needed to add a 'number' of controls, and just couldn't get my head around the syntax. This article worked first time! :)
ReplyCreate Controls Dynamically (VB6)
Posted by Legacy on 08/22/2003 12:00amOriginally posted by: stewart
-
Reply
-
ReplyUse Dynamically created and destroyed controls to avoid 255 conrol limit?
Posted by hbielich on 02/21/2006 08:35pmRe: destroy the controls
Posted by manoj_tuli on 05/11/2004 03:17amControls.Remove ("cmd1")ReplyLoading, Please Wait ...