Word 用户窗体中的复选框控件

发布于 2024-08-03 11:43:05 字数 1564 浏览 1 评论 0原文

我有一个带有复选框和两个命令控件的 Word 用户窗体 (Word 2007) - 确定和取消。

当从宏菜单或指定的图标激活表单时,我单击复选框,但没有任何反应。当我单击“确定”按钮时,会出现一条消息,告诉我我还没有选择任何内容!当我单击“取消”按钮时,表单将卸载。

复选框由 Click 事件组成,该事件从选中状态切换到取消选中状态。还有一个全选复选框。单击时,所有其他复选框都会被选中或取消选中。当取消选中其他复选框之一时,SelectAll 复选框也将取消选中。

当我从 VBE 激活表单时,一切正常。

这是我正在谈论的内容的示例:

Sub Loadform()
Load UserForm1
UserForm1.Show
End Sub

Private Sub btnCancel_Click()
Unload Me
End Sub

Private Sub btnOK_Click()
If Me.CheckBox2.Value = True And Me.CheckBox3.Value = True Then
    MsgBox "All checkboxes are checked"
ElseIf Me.CheckBox2.Value = True Then
    MsgBox Me.CheckBox2.Name & " is checked"
ElseIf Me.CheckBox3.Value = True Then
    MsgBox Me.CheckBox3.Name & " is checked"
ElseIf Me.CheckBox2.Value = False And Me.CheckBox3.Value = False Then
    MsgBox "You haven't selected any checkboxes."
End If

End Sub

Private Sub CheckBox2_Click()
If Me.CheckBox2.Value = True Then
    Me.CheckBox2.Value = False
    Me.ckbSelectAll.Value = False
Else
    Me.CheckBox2.Value = True
End If
End Sub

Private Sub CheckBox3_Click()
If Me.CheckBox3.Value = True Then
    Me.CheckBox3.Value = False
    Me.ckbSelectAll.Value = False
Else
    Me.CheckBox3.Value = True
End If
End Sub

Private Sub ckbSelectAll_Click()
If Me.ckbSelectAll.Value = True Then
    Me.ckbSelectAll.Value = False
Else
    Me.ckbSelectAll.Value = True
End If
If ckbSelectAll.Value = False Then
    Me.CheckBox2.Value = False
    Me.CheckBox3.Value = False
Else
    Me.CheckBox2.Value = True
    Me.CheckBox3.Value = True
End If
End Sub

I have a Word UserForm (Word 2007) with checkboxes and two command controls - Ok and Cancel.

When the form is activated from the macro menu or from an assigned icon, and I click on the checkboxes nothing happens. When I click on the OK button a message appears telling me that I haven't selected anything! When I click on the Cancel button the form unloads.

The checkboxes consist of the Click event which toggles from checked to uncheck. There's also a SelectAll checkbox. When clicked all the other checkboxes are checked or unchecked. When one of the other checkboxes is unchecked the SelectAll checkbox is also unchecked.

When I activate the form from the VBE everything works.

Here's a sample of what I'm talking about:

Sub Loadform()
Load UserForm1
UserForm1.Show
End Sub

Private Sub btnCancel_Click()
Unload Me
End Sub

Private Sub btnOK_Click()
If Me.CheckBox2.Value = True And Me.CheckBox3.Value = True Then
    MsgBox "All checkboxes are checked"
ElseIf Me.CheckBox2.Value = True Then
    MsgBox Me.CheckBox2.Name & " is checked"
ElseIf Me.CheckBox3.Value = True Then
    MsgBox Me.CheckBox3.Name & " is checked"
ElseIf Me.CheckBox2.Value = False And Me.CheckBox3.Value = False Then
    MsgBox "You haven't selected any checkboxes."
End If

End Sub

Private Sub CheckBox2_Click()
If Me.CheckBox2.Value = True Then
    Me.CheckBox2.Value = False
    Me.ckbSelectAll.Value = False
Else
    Me.CheckBox2.Value = True
End If
End Sub

Private Sub CheckBox3_Click()
If Me.CheckBox3.Value = True Then
    Me.CheckBox3.Value = False
    Me.ckbSelectAll.Value = False
Else
    Me.CheckBox3.Value = True
End If
End Sub

Private Sub ckbSelectAll_Click()
If Me.ckbSelectAll.Value = True Then
    Me.ckbSelectAll.Value = False
Else
    Me.ckbSelectAll.Value = True
End If
If ckbSelectAll.Value = False Then
    Me.CheckBox2.Value = False
    Me.CheckBox3.Value = False
Else
    Me.CheckBox2.Value = True
    Me.CheckBox3.Value = True
End If
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘然心甜 2024-08-10 11:43:05

在您的点击事件处理程序中,例如

Private Sub CheckBox3_Click()

...

End Sub

您检查复选框是否已选中(即值为 true),如果是,则将值设置为 false。这意味着复选框一旦被选中就总是被取消选中(或者总是在取消选中时被选中),从而使复选框看起来无法正常工作。

这是一个启动单击事件处理程序的示例(我已经很长时间没有编写VBA了,但我认为以下内容很好。现在将测试...)

Private Sub CheckBox2_Click()
    ' If checkbox2 is checked but checkbox3 is not, ' 
    ' uncheck the select all checkbox '
    If CheckBox2 And Not CheckBox3Then
        ckbSelectAll = False
    End If
End Sub

如果您需要任何进一步的帮助或提示,请发表评论

In your click event handlers, such as

Private Sub CheckBox3_Click()

...

End Sub

you check to see if the checkbox is checked (i.e. the value is true) and if it is, set the value to false. This means that a checkbox will always be unchecked as soon as it is checked (or always checked as soon as it is unchecked), giving the appearance that checkboxes don't work properly.

Here's an example click event handler to start (I haven't written VBA for a long time, but I think the following is fine. Will test now...)

Private Sub CheckBox2_Click()
    ' If checkbox2 is checked but checkbox3 is not, ' 
    ' uncheck the select all checkbox '
    If CheckBox2 And Not CheckBox3Then
        ckbSelectAll = False
    End If
End Sub

If you need any further help or tips, then please leave a comment

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文