Pregunta

Tengo el siguiente 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

Estoy buscando consolidar esto en un sub sin tener ningún código repetido (todos los subs actualmente tienen btnSubmitUIDisabled() y lblStatusClear())

Pensé en un CASE declaración pero eso también tendría código repetitivo.Esta es una aplicación WPF y todos los eventos "TextChanged" se encuentran en el xaml, por lo que no hay "Manejadores" al final de cada sub.

Gracias de antemano.

¿Fue útil?

Solución

Bueno, para empezar, puedes poner las llamadas en cascada:

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

Yo personalmente los nombraría de manera diferente en ese momento: haga que el método diga lo que dice. hace en lugar de lo que reacciona a - pero eso es solo una diferencia de opinión de larga data entre Visual Studio y yo.

Otros consejos

Pseudocódigo que no es muy propio de 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

No es bonito, sino un método.

EDITAR:Una cosa que no mencioné es que usted querrá enviar el remitente a un tipo de cuadro de texto para poder obtener la propiedad del nombre.

Si lo quiere todo en un solo método, la respuesta de @spinon es unidireccional.Otra es escribir un método de extensión para reducir la cantidad de declaraciones OrElse que necesita escribir y hace que el código sea un poco más legible.

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 bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top