Author : Deep Softwares
Date Submitted : 8/12/2006
Category : Forms
Compatibility : VB 6,VB 5
This code has been accessed 5374 times.
Task : Add Controls (i.e. TextBox) on the form dynamically during runtime
Declarations
Code
Private txtObject As TextBox
Private tValue As Long, cIndx As Integer, ctrlName As String
Private Sub Command1_Click()
cIndx = cIndx + 1
tValue = tValue + 400
ctrlName = "DynTxtBx" & CStr(cIndx)
Set txtObject = Form1.Controls.Add("VB.TextBox", ctrlName)
With txtObject
.Visible = True
.Left = 250
.Width = 4000
.Height = 250
.Top = tValue
.Text = "This is a dynamically created TextBox No." & CStr(cIndx)
End With
If Not Command2.Enabled Then Command2.Enabled = True
End Sub
Private Sub Command2_Click()
Form1.Controls.Remove ctrlName
cIndx = cIndx - 1: tValue = tValue - 400
ctrlName = "DynTxtBx" & CStr(cIndx)
If cIndx = 0 Then Command2.Enabled = False
End Sub