Hi,
I am trying to generate controls dynamically at runtime. I am able to do that using the code below but I don't have a clue how to handle events from these controls.
Any help would really be appreciated.
Sub test4()
' Note: a textbox has no caption, hence the caption string is left null
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.checkbox.1", 2, 2, 175, 20, "1")
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.textbox.1", 2, 2, 175, 20, "")
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.label.1", 2, 2, 175, 20, "3")
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.optionbutton.1", 2, 2, 175, 20, "4")
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.togglebutton.1", 2, 70, 175, 20, "5")
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.commandbutton.1", 2, 70, 175, 20, "6")
frm1.Show
End Sub
Sub AddCheckBox(intCount As Integer, strControl As String, intLeft As Integer, intTop As Integer, _
intWidth As Integer, intHeight As Integer, strCaption As String)
'Dim ctl As Control
Dim mycmd As Control
'strControl = "Forms.checkbox.1"
Set mycmd = Controls.Add(strControl)
mycmd.Left = intLeft
mycmd.Top = intTop + 20 * intCount
mycmd.Width = intWidth
mycmd.Height = intHeight
If strCaption <> "" Then
mycmd.Caption = strCaption
End If
mycmd.Visible = True
' Me.ScrollBars = fmScrollBarsVertical
With Me
.ScrollBars = fmScrollBarsVertical
.ScrollHeight = intTop + 40 + 20 * intCount
End With
End Sub
I am trying to generate controls dynamically at runtime. I am able to do that using the code below but I don't have a clue how to handle events from these controls.
Any help would really be appreciated.
Sub test4()
' Note: a textbox has no caption, hence the caption string is left null
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.checkbox.1", 2, 2, 175, 20, "1")
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.textbox.1", 2, 2, 175, 20, "")
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.label.1", 2, 2, 175, 20, "3")
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.optionbutton.1", 2, 2, 175, 20, "4")
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.togglebutton.1", 2, 70, 175, 20, "5")
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.commandbutton.1", 2, 70, 175, 20, "6")
frm1.Show
End Sub
Sub AddCheckBox(intCount As Integer, strControl As String, intLeft As Integer, intTop As Integer, _
intWidth As Integer, intHeight As Integer, strCaption As String)
'Dim ctl As Control
Dim mycmd As Control
'strControl = "Forms.checkbox.1"
Set mycmd = Controls.Add(strControl)
mycmd.Left = intLeft
mycmd.Top = intTop + 20 * intCount
mycmd.Width = intWidth
mycmd.Height = intHeight
If strCaption <> "" Then
mycmd.Caption = strCaption
End If
mycmd.Visible = True
' Me.ScrollBars = fmScrollBarsVertical
With Me
.ScrollBars = fmScrollBarsVertical
.ScrollHeight = intTop + 40 + 20 * intCount
End With
End Sub