繁体   English   中英

使用动态添加到用户表单的复选框

[英]Use checkbox dynamically added to userform

我使用以下代码将CheckBox1添加到我的UserForm

Private Sub UserForm_Initialize()
    Dim opt As Variant
    Set opt = UserForm1.Controls.Add("Forms.checkbox.1", "CheckBox1", True)
End Sub

现在,当我单击CommandButton我想检查CheckBox1是否已选中:

Private Sub CommandButton1_Click()
    If CheckBox1.Value = False Then
        MsgBox "F"
    End If
End Sub

但是这段代码不起作用; 我认为是因为复选框是动态添加的。
这只是解决问题的代码的简化。

这就是你的想法:

Option Explicit

Private Sub UserForm_Initialize()
    Dim opt As Variant
    Set opt = Me.Controls.Add("Forms.checkbox.1", "CheckBox1", True)
End Sub

Private Sub CommandButton1_Click()
    If Not Me.Controls("CheckBox1") Then
        MsgBox "F"
    End If
End Sub

但是,根据您的经验和编写更好代码的愿望,您可能决定在使用 Forms 时遵循一些 MVC 模式。 阅读这些以了解更多关于它的想法:

它需要如下

Private Sub CommandButton1_Click()
If Me.Controls("Checkbox1").Value = False Then
MsgBox "F"
End If
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM