压缩vb.net代码

发布于 2024-09-11 04:33:27 字数 1196 浏览 4 评论 0原文

我有以下代码:

  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”。

提前致谢。

I have the following code:

  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

I am looking to consolidate this into one sub without having any repeating code (all subs currently hold btnSubmitUIDisabled() and lblStatusClear())

I thought about a CASE statement but that would also have repetitive code. This is a WPF application and all the "TextChanged" events are located in the xaml, thus no "Handles" at the end of each sub.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三月梨花 2024-09-18 04:33:28

伪代码不太像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

不漂亮,但一种方法。

编辑:我没有提到的一件事是您希望将发送者转换为文本框类型,以便您可以获得 name 属性。

Pseudo code as not that much of a 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

Not pretty but one method.

EDIT: One thing I didn't mention is you would want to cast the sender to a textbox type so that you can get the name property.

神仙妹妹 2024-09-18 04:33:28

如果您希望通过一种方法实现所有内容,那么@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

If you want it all in one method then @spinon answer is one way. Another is write an extension method to reduce the amount of OrElse statements you need to write and it makes the code a bit more readable.

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
倒带 2024-09-18 04:33:27

一方面,您可以级联调用:

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 之间长期存在的意见分歧。

Well for one thing you can cascade the calls:

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

I would personally name them differently at that point - make the method say what it does rather than what it reacts to - but that's just a long-running difference of opinion between myself and Visual Studio.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文