2016-12-06 65 views
0

如何将多个复选框添加到userForm框架?这似乎是一个微不足道的事情,但我的代码只是为数组中的最后一项生成复选框。动态添加复选框到框架上vba

Private Sub btnGenerate_Click() 
Dim i As Long 
Dim lic As licence 
Dim temp As Variant 
Dim desc As String 
Dim chkbox As MSForms.CheckBox 
Dim str As String 

For Each lic In licenceCollection 
    temp = lic.getClause 
Next lic 

For i = LBound(temp) To UBound(temp) 
    'Debug.Print temp(i) 
    desc = "Future-Sampling " & i 
    'Utility.createCheckBoxes temp(i), desc 
    Set chkbox = licenceForm.resultFrame.Controls.Add("Forms.Checkbox.1", desc) 
    chkbox.Caption = temp(i) 
    chkbox.Value = desc 
    chkbox.Width = "450" 
    chkbox.Height = "50" 
    chkbox.WordWrap = True 
    chkbox.Value = False 
    chkbox.GroupName = "Future Sampling" 
Next 
End Sub 

这里的任何建议非常感谢。提前致谢。

+0

您需要在最后一个接着我... – Rdster

+0

@Rdster“下一步” +变量用来只为保留的范围值i循环。 – holmicz

+1

@holmicz - 它与范围无关。 'Next'和'Next i'之间没有任何功能上的区别。这完全是一个偏好和编码风格的问题。 – Comintern

回答

0

我认为问题在于,您在相同的位置添加组件,因此它是重叠的,您只能看到最后一个,不是吗?如果我没有记错,您需要调整lefttop坐标。

1

它们都被创建得很好,但是只能看到最后一个的原因是它们在添加时都默认堆叠在默认位置。你需要将它们与.Top.Left性质定位:

Dim xPos As Long 
For i = LBound(temp) To UBound(temp) 
    desc = "Future-Sampling " & i 
    Set chkbox = Me.Controls.Add("Forms.Checkbox.1", desc) 
    With chkbox 
     .Top = xPos 
     .Caption = temp(i) 
     .Value = desc 
     .Width = 450 
     .Height = 24 
     .WordWrap = True 
     .Value = False 
     .GroupName = "Future Sampling" 
     xPos = xPos + 24 
    End With 
Next 
0

感谢所有,似乎这样的伎俩!我从来没有想通这个续

以下修改后的代码片段,

 For i = LBound(temp) To UBound(temp) 
       desc = "Future-Sampling " & i 
       Set chkbox = Me.resultFrame.Controls.Add("Forms.Checkbox.1", desc) 
       With chkbox 
        .Top = xPos 
        .Caption = temp(i) 
        .Value = desc 
        .Width = 450 
        .Height = 24 
        .WordWrap = True 
        .Value = False 
        .GroupName = "Future Sampling" 
        xPos = xPos + 24 
       End With 


     Next i 
+1

然后接受@Comintern的回答! – user3598756