VBA 用户窗体根据组合框选择禁用和更改文本框的背景色

问题描述 投票:0回答:1

我有一个带有组合框的用户表单。当我从组合框下拉列表中选择“服务”或“修复”时,我想禁用并灰显几个文本框和组合框。当我选择“系统”时,我希望它们恢复到原来的属性。我不知道该怎么做。另外,我在 arr = (txtQuoteApproved 等) 行上遇到语法错误。我们将非常感谢您的帮助:)。

Private Sub cboOrderType_Change()
    Dim c As String
    Dim arr() As String
    c = Me.cboOrderType.Text
    arr = Array(txtApprovedQuote, txtPMAssign, txtSOATeam, txtFinance, txtApproved, txtSOACust, txtSOAE10, txtInvPaid, _
    txtShipReq, txtBOL, txtQuote, txtBMTH, txtTransOrdNum, txtDiamond, cboTE, txtPM, txtEE, cboSCodeDes, txtSCode, _
    txtSys, cboShipVia, cboShipType, cboShipChrgs, txtShipInst, txtCost, txtMargin, txtLT)
    If c = "Service" Then
            arr.Enabled = False 'Error: invalid qualifier on "arr"
            arr.BackColor = &HE0E0E0
    ElseIf c = "Repair" Then
            arr.Enabled = False
            arr.BackColor = &HE0E0E0
    ElseIf c = "System" Then
        'exit sub? How do I revert the array back to the original properties?
    End If
End Sub
vba windows combobox userform
1个回答
0
投票

如果你想更新多个控件,你需要一个循环

这对我有用:

Private Sub cboOrderType_Change()
    
    Dim arr, clr As Long, enable As Boolean
    
    arr = Array(TextBox1, TextBox2)
    
    Select Case Me.cboOrderType.Text
        Case "Service", "Repair"
            enable = False
            clr = vbGrayText
        Case "System"
            enable = True
            clr = vbWhite
        Case Else
            Exit Sub   'No match so just exit here, unless
                       '  there's some other fall-back required
    End Select
    
    For Each c In arr
        c.Enabled = enable
        c.BackColor = clr
    Next c
   
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.