2013-10-30 55 views
0

這是我寫的代碼,當條件滿足時鎖定用戶窗體文本框。它在重複的形式來明確低效:爲用戶自定義文本框設置重複屬性:這可以更輕鬆地完成嗎?

With txtESPPStock 
     .Locked = True 
     .BackColor = &H80000000 
     .value = "" 
     .Enabled = False 
    End With 
    With txtChildLife 
     .Locked = True 
     .BackColor = &H80000000 
     .value = "" 
     .Enabled = False 
    End With 
    With txtHealthcareFSA 
     .Locked = True 
     .BackColor = &H80000000 
     .value = "" 
     .Enabled = False 
    End With 
    With txtLtdPlusBuyUp 
     .Locked = True 
     .BackColor = &H80000000 
     .value = "" 
     .Enabled = False 
    End With 
    With txtMedicalDental 
     .Locked = True 
     .BackColor = &H80000000 
     .value = "" 
     .Enabled = False 
    End With 
    With txtSpouseLife 
     .Locked = True 
     .BackColor = &H80000000 
     .value = "" 
     .Enabled = False 
    End With 
    With txtStdPlusBuyUp 
     .Locked = True 
     .BackColor = &H80000000 
     .value = "" 
     .Enabled = False 
    End With 
    With txtSupplementalLife 
     .Locked = True 
     .BackColor = &H80000000 
     .value = "" 
     .Enabled = False 
    End With 
    With txtVision 
     .Locked = True 
     .BackColor = &H80000000 
     .value = "" 
     .Enabled = False 
    End With 
    With txt401kReg 
     .Locked = True 
     .BackColor = &H80000000 
     .value = "" 
     .Enabled = False 
    End With 

由於所有的內部設置是一樣的,好像如果我只是足夠聰明,我可以在許多行較少它的代碼。我想是說,對於每一個文本框,像:

With txtESPPStock 
    Call GetProperties 
End With 

甚至更​​好,獲得所有需要的屬性設置爲坐在一個數組,所以我可以做類似的特性:

For txtFieldNumber = 0 To 15 
    For PropertySettings = 0 To 3 
     ArrayValue(txtFieldNumber, PropertySettings) = getproperty(PropertySettings) 
    Next PropertySettings 
Next txtFieldNumber 

也許我與這最後一個的結構很接近,但我似乎缺乏一些知識,使我清楚如何做到這一點。任何人都可以幫我理清我嗎?


以同樣的方式學習,你可以遍歷數組通過範圍循環後,我能瘦下來的代碼如我所料,讓一個工具,它是由其他用戶窗體重複使用而無需重新編碼的設置:

Sub SetLockedFields(ByRef arrLockedFields() As Variant) 

    Dim varFieldToLock As Variant 
     For Each varFieldToLock In arrLockedFields 
      With frmPaycheckEntry.Controls(varFieldToLock) 
       .Locked = True 
       .BackColor = &H80000000 
       .value = "" 
       .Enabled = False 
      End With 
     Next 

End Sub 

要做到在我的第一個代碼示例所示的所有工作上面,我現在只是放置文本字段變量數組,並通過他們,就像這樣:

Private Sub UserForm_Initialize() 

    Dim arrLockedFields() As Variant 
      . 
      . 
      . 

     If <condition is met> Then 
      ReDim arrLockedFields(1 To 11) 
      arrLockedFields = Array("txtBasicLife", "txtChildLife", _ 
           "txtESPPStock", "txtHealthcareFSA", _ 
           "txtLtdPlusBuyUp", "txt401kReg", _ 
           "txtMedicalDental", "txtSpouseLife", _ 
           "txtStdPlusBuyUp", "txtSupplementalLife", _ 
           "txtVision") 
     End If 

    Call SetLockedFields(arrLockedFields) 

End Sub 

回答

1

是,你可以使用循環來避免重複的代碼。

Sub t1() 
    Dim ctrl As Control 
    For Each ctrl In UserForm1.Controls 
     'Check if the control is a textbox 
     If TypeName(ctrl) = "TextBox" Then 
      With UserForm1.Controls(ctrl.Name) 
       .Locked = True 
       .BackColor = &H80000000 
       .Value = "" 
       .Enabled = False 
      End With 
     End If 
    Next 
End Sub 

又如:當你有一定的名字

Private Sub CommandButton1_Click() 
    Dim names() As Variant 
    Dim name As Variant 
    names = Array("TextBox1", "TextBox2") 
    For Each name In names 
     With UserForm1.Controls(name) 
      .Locked = True 
      .BackColor = &H80000000 
      .Value = "" 
      .Enabled = False 
     End With 
    Next 
End Sub 
+0

這是偉大的。我不知道你使用的控制方法。我會稍微玩一下。不幸的是,只有一些文本框(不是全部)需要被鎖定,所以我需要一種調用相關的方法。 +1 Upvote,但不完全是我正在尋找的答案。 –

+0

是的,我只是說明一般的方法。哪些是你正在尋找的具體的? – sam092

+0

如果您不需要全部鎖定,那麼創建另一個具有例外的數組,然後在sam092的循環內添加一個簡單的'if'語句。 – L42

相關問題