Hi guys, I Really need your help......
I am new to VBA and I encountered a problem like this:
I use class module to add 20 CheckBoxes dynamically on a form but when I click the CheckBox,it doesn't work!
It seems that the CheckBox is not associated with class module.
The form code is as below :
Note1: The name of the form is " myForm" .
Note2: The name of the frame on myForm is " myFrame".
Code
Private Sub UserForm_Initialize()
Dim i As Integer
Dim CheckBox_Voc() As New Class1
ReDim CheckBox_Voc(1 To 20)
Dim checkbox_top, checkbox_left As Integer
Dim checkbox_Row_num As Integer
Dim Row_order As Integer
Dim k As Integer
Const checkbox_width = 120
Const checkbox_height = 35
Const checkbox_gap = 15
checkbox_Row_num = 4
For i = 1 To 20
Set CheckBox_Voc(i).my_CheckBox = myForm.myFrame.Controls.Add("Forms.CheckBox.1")
Row_order = (i \ 5) + 1
If (i Mod 5 = 0) Then Row_order = i \ 5
k = i - (Row_order - 1) * 5
CheckBox_Voc(i).my_CheckBox.Top = 20 + (Row_order - 1) * (checkbox_height + checkbox_gap)
CheckBox_Voc(i).my_CheckBox.Left = 20 + (k - 1) * (checkbox_width + checkbox_gap)
CheckBox_Voc(i).my_CheckBox.Font.Size = 14
CheckBox_Voc(i).my_CheckBox.Caption = " " + voc_array(i - 1) '填入單字
CheckBox_Voc(i).my_CheckBox.Width = checkbox_width
CheckBox_Voc(i).my_CheckBox.Height = checkbox_height
Next i
'------------------------------------
End Sub
Display More
AND I use a class module (named "Class1") as below:
Code
Public WithEvents my_CheckBox As msforms.CheckBox
Private Sub my_CheckBox_Click()
MsgBox "Hello"
End Sub
Seeking your help. Please advise. Thanks in advance.