Pergunta

Eu tenho o seguinte código:

  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

Estou procurando consolidar isso em um sub sem ter nenhum código repetido (todos os subs atualmente possuem btnSubmitUIDisabled() e lblStatusClear())

pensei em um CASE instrução, mas isso também teria código repetitivo.Este é um aplicativo WPF e todos os eventos "TextChanged" estão localizados no xaml, portanto, não há "Handles" no final de cada sub.

Desde já, obrigado.

Foi útil?

Solução

Bem, para começar, você pode colocar as chamadas em cascata:

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

Eu pessoalmente os nomearia de forma diferente nesse ponto - faça o método dizer o que é faz em vez do que reage a - mas isso é apenas uma longa diferença de opinião entre mim e o Visual Studio.

Outras dicas

Pseudo-código como um cara não muito 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

Não é bonito, mas um método.

EDITAR:Uma coisa que não mencionei é que você deseja converter o remetente para um tipo de caixa de texto para poder obter a propriedade name.

Se você quiser tudo de um só método, a resposta do @spinon é unilateral.Outra é escrever um método de extensão para reduzir a quantidade de instruções OrElse que você precisa escrever e tornar o código um pouco mais legível.

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top