Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to disable multiple buttons at one time?

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.

like image 395
Mike Avatar asked Apr 08 '11 16:04

Mike


People also ask

How do I make buttons disabled?

You can disable the <button> element in HTML by adding the disabled attribute to the element. The disabled attribute is a boolean attribute that allows you to disable an element, making the element unusable from the browser.

How do I make a button disabled after one click?

1.1 To disable a submit button, you just need to add a disabled attribute to the submit button. $("#btnSubmit"). attr("disabled", true); 1.2 To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.

How do you disable a button for 30 seconds?

Here use setTimeout, to enable it after 30 seconds. In the anonymus function of setTimeout. Modify the DOM property is also called disabled and is a boolean that takes true or false.


1 Answers

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
like image 68
Thomas Avatar answered Oct 01 '22 02:10

Thomas