質問

次のコードがあります。

  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
.

これを繰り返しコードを持たずにこれを1つのサブに統合しようとしています(すべてのサブ現在、btnSubmitUIDisabled()lblStatusClear()

CASEステートメントについて考えたが、それはまた繰り返しコードを有するであろう。これはWPFアプリケーションであり、すべての「TextChanged」イベントはXAMLにあり、したがって各サブの末尾に「ハンドル」はありません。

事前にありがとう。

役に立ちましたか?

解決

電話をカスケードすることができる1つのこと:

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の間の意見

他のヒント

VB Guyの大部分のように疑似コード:

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
.

きれいではないが1つの方法。

編集:私が言及しなかったことの1つは、名前のプロパティを取得できるように、送信者をテキストボックスの種類にキャストすることをお勧めします。

あなたがそれを1つの方法ですべて望むなら、@spinonの答えは一つの方法です。もう1つは、書き込みに必要な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
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top