有没有办法同时禁用多个按钮?

发布于 2024-10-31 02:54:28 字数 107 浏览 0 评论 0原文

我有一个 vba 用户窗体,上面有 36 个按钮。当我的电子表格之一上的值达到特定数字时,我想禁用所有按钮。现在,每次单击按钮,我引用的电子表格上的数字就会增加一。当数量达到三个时,我想禁用所有按钮。

I have a vba userForm that has 36 buttons on it. I would like to disable all buttons when a value on one of my spreadshets reaches a certain number. Right now with each click of a button a number goes up by one on the spreadsheet I'm referencing. When the number reaches three I would like to disable all buttons.

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

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

发布评论

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

评论(3

∞觅青森が 2024-11-07 02:54:28
Dim oCtrl As Control
Dim oButton As CommandButton

For Each oCtrl In MyForm.Controls
    If oCtrl.Tag = "SomeValue" Then
        Set oButton = oCtrl
        oButton.Enabled = False
    End If
Next

Set oCtrl = Nothing
Set oButton = Nothing

如果您不想禁用其他按钮,解决方案是使用 Tag 属性。将您想要同时启用或禁用的所有按钮的 Tag 属性设置为相同的值。然后您可以检查该值并启用/禁用它们。另一种方法是将它们命名为相同的前缀或后缀,并在代码中进行检查。

添加

顺便说一句,Control 对象没有 Enabled 属性。因此,您必须将其“投射”到 CommandButton 才能禁用它。显然,Control 对象确实具有 Enabled 属性,但它不会在智能感知中显示。但是,您仍然应该尝试将 Control 转换为 CommandButton,以确保这就是您所拥有的。这是一个扩展版本:

Dim oCtrl As Control
Dim oButton As CommandButton

For Each oCtrl In MyForm.Controls
    If oCtrl.Tag = "SomeValue" Then
        On Error Resume Next
        Set oButton = oCtrl
        On Error GoTo 0

        If Not oButton Is Nothing Then
            oButton.Enabled = False
        End If
    End If
Next

Set oCtrl = Nothing
Set oButton = Nothing
Dim oCtrl As Control
Dim oButton As CommandButton

For Each oCtrl In MyForm.Controls
    If oCtrl.Tag = "SomeValue" Then
        Set oButton = oCtrl
        oButton.Enabled = False
    End If
Next

Set oCtrl = Nothing
Set oButton = Nothing

If you have other buttons which you do not want disabled, the solution is to use the Tag property. Set the Tag property on all the buttons which you will want to enable or disable together to the same value. Then you can check for that value in a look and enable/disable them. Another way is to name them the same prefix or suffix and check for that in your code.

Addition

Btw, the Control object does not have an Enabled property. So you must "cast" it to a CommandButton to disable it. Apparently a Control object does have an Enabled property but it does not show in intellisense. However, you should still try to cast the Control to a CommandButton to ensure that's what you have. Here's an expanded version:

Dim oCtrl As Control
Dim oButton As CommandButton

For Each oCtrl In MyForm.Controls
    If oCtrl.Tag = "SomeValue" Then
        On Error Resume Next
        Set oButton = oCtrl
        On Error GoTo 0

        If Not oButton Is Nothing Then
            oButton.Enabled = False
        End If
    End If
Next

Set oCtrl = Nothing
Set oButton = Nothing
我们的影子 2024-11-07 02:54:28

将所有按钮放在一个 Frame 对象中,然后禁用整个框架。这也将禁用框架内的所有内容。

或者,根据您的最后一个问题,您可以使用以下代码:

Dim counter As Integer

Private Sub btn1_Click()
    CaptureImage (btn1.Name)
End Sub

Private Sub btn2_Click()
    CaptureImage (btn2.Name)
End Sub

Private Sub btn3_Click()
    CaptureImage (btn3.Name)
End Sub

Private Sub UserForm_Activate()
    counter = 1
End Sub

Private Sub CaptureImage(ByVal btnName As String)
    Controls("capture" & counter).Picture = Controls(btnName).Picture
    counter = counter + 1
    If counter > 3 Then
        DisableButtons
    End If
End Sub

Private Sub DisableButtons()
    Dim ctl As Control
    For Each ctl In UserForm1.Controls
        If Left(ctl.Name, 3) = "btn" Then
            ctl.Enabled = False
        End If
    Next
End Sub

理想情况下,您希望像托马斯建议的那样将 Control 对象强制转换为按钮。

Place all the buttons in a Frame object, then just disable the entire frame. This will also disable everything inside the frame.

Alternatively, based on your last question, you could use this code:

Dim counter As Integer

Private Sub btn1_Click()
    CaptureImage (btn1.Name)
End Sub

Private Sub btn2_Click()
    CaptureImage (btn2.Name)
End Sub

Private Sub btn3_Click()
    CaptureImage (btn3.Name)
End Sub

Private Sub UserForm_Activate()
    counter = 1
End Sub

Private Sub CaptureImage(ByVal btnName As String)
    Controls("capture" & counter).Picture = Controls(btnName).Picture
    counter = counter + 1
    If counter > 3 Then
        DisableButtons
    End If
End Sub

Private Sub DisableButtons()
    Dim ctl As Control
    For Each ctl In UserForm1.Controls
        If Left(ctl.Name, 3) = "btn" Then
            ctl.Enabled = False
        End If
    Next
End Sub

Ideally, you would want to cast the Control objects to Buttons like Thomas suggests.

暮年 2024-11-07 02:54:28

@Mike:尝试 --

If Sheets("Sheet1").Range("A1").Value = 3 Then 
    UserForm1.CommandButton1.Enabled = True 
Else 
    UserForm1.CommandButton1.Enabled = False 
End If

(将 Sheet1A1UserForm1CommandButton1 替换为适合您工作簿的正确值)

@Mike: Try --

If Sheets("Sheet1").Range("A1").Value = 3 Then 
    UserForm1.CommandButton1.Enabled = True 
Else 
    UserForm1.CommandButton1.Enabled = False 
End If

(Replace Sheet1, A1, UserForm1 and CommandButton1 with the correct ones for your workbook)

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