2010-07-23 136 views
0

我有以下代碼:冷凝vb.net代碼

Private Sub txtFileFromLocation_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) 
    MachineNameUIDisabled() 
    ServiceNameUIDisabled() 
    ToLocationUIDisabled() 
    btnSubmitUIDisabled() 
    lblStatusClear() 
    End Sub 

    Private Sub txtMachineName_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) 
    ServiceNameUIDisabled() 
    ToLocationUIDisabled() 
    btnSubmitUIDisabled() 
    lblStatusClear() 
    End Sub 

    Private Sub txtServiceName_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) 
    ToLocationUIDisabled() 
    btnSubmitUIDisabled() 
    lblStatusClear() 
    End Sub 


    Private Sub txtFilesToLocation_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) 
    btnSubmitUIDisabled() 
    lblStatusClear() 
    End Sub 

我期待整合成一個子此,而無需任何重複代碼(所有潛艇目前持有btnSubmitUIDisabled()lblStatusClear()

我想關於CASE聲明,但也會有重複的代碼。這是一個WPF應用程序,所有「TextChanged」事件都位於xaml中,因此在每個子結尾處沒有「Handles」。

在此先感謝。

回答

4

好一兩件事可以級聯呼叫:

Private Sub txtFileFromLocation_TextChanged(ByVal sender As Object, _ 
              ByVal e As TextChangedEventArgs) 
    MachineNameUIDisabled() 
    txtMachineName_TextChanged() 
End Sub 

Private Sub txtMachineName_TextChanged(ByVal sender As Object, _ 
             ByVal e As TextChangedEventArgs) 
    ServiceNameUIDisabled() 
    txtServiceName_TextChanged() 
End Sub 

Private Sub txtServiceName_TextChanged(ByVal sender As Object, _ 
             ByVal e As TextChangedEventArgs) 
    ToLocationUIDisabled() 
    txtFilesToLocation_TextChanged() 
End Sub 


Private Sub txtFilesToLocation_TextChanged(ByVal sender As Object, 
              ByVal e As TextChangedEventArgs) 
    btnSubmitUIDisabled() 
    lblStatusClear() 
End Sub 

我會親自他們的名字不同,在這一點上 - 使法說什麼確實而不是什麼做出反應,以 - 但這只是我和Visual Studio之間長期存在的意見分歧。

+0

我剛剛點擊「發佈您的答案」與完全相同的東西,當橙色的酒吧出現在頂端... – Vedran 2010-07-23 20:56:03

0

僞代碼,不是太大的VB的傢伙:

Private Sub txt_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) 

    if (sender.Name = txtServiceName) 
    MachineNameUIDisabled() 

    if (sender.Name = txtServiceName or sender.Name = txtMachineName) 
    ServiceNameUIDisabled() 

    if (sender.Name = txtServiceName or sender.Name = txtMachineName or sender.Name = txtFileFromLocation) 
    ToLocationUIDisabled() 

    btnSubmitUIDisabled() 
    lblStatusClear() 
    End Sub 

不漂亮,但一個方法。

編輯:我沒有提到的一件事是你想要將發件人轉換爲文本框類型,以便您可以獲取名稱屬性。

0

如果你想在一個方法中使用所有方法,那麼@spinon答案是一種方法。另一種方法是編寫一個擴展方法來減少需要編寫的OrElse語句的數量,並使代碼更具可讀性。

Private Sub txt_TextChanged(ByVal sender As Object, 
          ByVal e As TextChangedEventArgs 
          ) 

    Dim textBox = TryCast(sender, TextBox) 

    If textBox IsNot Nothing Then 

     If textBox.IsOneOf(txtServiceName) Then 
      MachineNameUIDisabled() 
     End If 

     If textBox.IsOneOf(txtServiceName, txtMachineName) Then 
      ServiceNameUIDisabled() 
     End If 

     If textBox.IsOneOf(txtServiceName, txtMachineName, txtFileFromLocation) Then 
      ToLocationUIDisabled() 
     End If 

     If textBox.IsOneOf(txtServiceName, txtMachineName, txtFileFromLocation, 
      txtFilesToLocation) Then 

      btnSubmitUIDisabled() 
      lblStatusClear() 

     End If 

    End If 

End Sub 

<Extension()> 
Public Function IsOneOf(ByVal value As Object, 
         ByVal ParamArray values() As Object 
         ) As Boolean 

    If value Is Nothing Then 
     Throw New ArgumentNullException("value") 
    End If 

    If values Is Nothing Then 
     Throw New ArgumentNullException("values") 
    End If 

    Return values.Contains(value) 

End Function