是否有任何短代码方法用于关系操作的条件语句?

时间:2015-03-13 04:56:44

标签: vb.net if-statement checkbox conditional-statements

我正在创建我的这个学校项目,一个洗衣店系统。 它包含许多复选框,以了解客户想要的特定服务。我的问题是,是否有任何编码方法来最小化我的条件if-elseif-endif语句?

我的代码如下所示:

    If chkWashDryFold.Checked And chkWashDryPress.Checked And chkPressOnly.Checked And chkBarong.Checked And chkCoat.Checked And chkJacket.Checked And chkPolo.Checked And chkBlouse.Checked And chkSkirt.Checked And chkDress.Checked And chkGown.Checked And chkChildrens.Checked And chkStuff.Checked And chkPants.Checked = True Then
    'statement goes here........
    ElseIf chkWashDryFold.Checked And chkWashDryPress.Checked And chkPressOnly.Checked And chkBarong.Checked And chkCoat.Checked And chkJacket.Checked And chkPolo.Checked And chkBlouse.Checked And chkSkirt.Checked And chkDress.Checked And chkGown.Checked And chkChildrens.Checked = True Then
    'statement goes here........
    ElseIf chkWashDryFold.Checked And chkWashDryPress.Checked And chkPressOnly.Checked And chkBarong.Checked And chkCoat.Checked And chkJacket.Checked And chkPolo.Checked And chkBlouse.Checked And chkSkirt.Checked And chkDress.Checked And chkGown.Checked And chkStuff.Checked = True Then
    'statement goes here........
    ElseIf chkWashDryFold.Checked And chkWashDryPress.Checked And chkPressOnly.Checked And chkBarong.Checked And chkCoat.Checked And chkJacket.Checked And chkPolo.Checked And chkBlouse.Checked And chkSkirt.Checked And chkDress.Checked And chkChildrens.Checked And chkStuff.Checked = True Then
   'statement goes here........ 
    ElseIf chkWashDryFold.Checked And chkWashDryPress.Checked And chkPressOnly.Checked And chkBarong.Checked And chkCoat.Checked And chkJacket.Checked And chkPolo.Checked And chkBlouse.Checked And chkSkirt.Checked And chkGown.Checked And chkChildrens.Checked And chkStuff.Checked = True Then
    'statement goes here........
    ElseIf chkWashDryFold.Checked And chkWashDryPress.Checked And chkPressOnly.Checked And chkBarong.Checked And chkCoat.Checked And chkJacket.Checked And chkPolo.Checked And chkBlouse.Checked And chkDress.Checked And chkGown.Checked And chkChildrens.Checked And chkStuff.Checked = True Then
    'statement goes here........
    ElseIf chkWashDryFold.Checked And chkWashDryPress.Checked And chkPressOnly.Checked And chkBarong.Checked And chkCoat.Checked And chkJacket.Checked And chkPolo.Checked And chkSkirt.Checked And chkDress.Checked And chkGown.Checked And chkChildrens.Checked And chkStuff.Checked = True Then
    'statement goes here........
    ElseIf chkWashDryFold.Checked And chkWashDryPress.Checked And chkPressOnly.Checked And chkBarong.Checked And chkCoat.Checked And chkJacket.Checked And chkBlouse.Checked And chkSkirt.Checked And chkDress.Checked And chkGown.Checked And chkChildrens.Checked And chkStuff.Checked = True Then  
    'statement goes here........
    ElseIf chkWashDryFold.Checked And chkWashDryPress.Checked And chkPressOnly.Checked And chkBarong.Checked And chkCoat.Checked And chkPolo.Checked And chkBlouse.Checked And chkSkirt.Checked And chkDress.Checked And chkGown.Checked And chkChildrens.Checked And chkStuff.Checked = True Then
    'statement goes here........
    ElseIf chkWashDryFold.Checked And chkWashDryPress.Checked And chkPressOnly.Checked And chkBarong.Checked And chkJacket.Checked And chkPolo.Checked And chkBlouse.Checked And chkSkirt.Checked And chkDress.Checked And chkGown.Checked And chkChildrens.Checked And chkStuff.Checked = True Then
    'statement goes here........
    ElseIf chkWashDryFold.Checked And chkWashDryPress.Checked And chkPressOnly.Checked And chkCoat.Checked And chkJacket.Checked And chkPolo.Checked And chkBlouse.Checked And chkSkirt.Checked And chkDress.Checked And chkGown.Checked And chkChildrens.Checked And chkStuff.Checked = True Then
    'statement goes here........
    End if

我的程序是当用户选中1复选框或2个复选框或3个复选框等等..

我的程序的示例图像,包含几个复选框: http://i.imgur.com/NiqU7XC.png

1 个答案:

答案 0 :(得分:2)

一种方法是进行一些重构。可以使用按位枚举的实例来存储每个复选框的已检查状态:

<Flags()> Public Enum Laundry As Integer
    WashDryFold = 1
    WashDryPress = 2
    PressOnly = 4
    'and so on ...(doubling the value each time)
End Enum

然后将复选框的Tag属性设置为适当的枚举值:

Public LaundrySettings As Laundry
Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    CheckBox1.Tag = Laundry.WashDryFold
    CheckBox2.Tag = Laundry.WashDryPress
    CheckBox3.Tag = Laundry.PressOnly
    'and so on ...
End Sub

当您准备好评估复选框时,只需从复选框中构建枚举值,然后使用选择块执行需要执行的操作。每个独特的复选框组合都会产生一个独特的值。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    LaundrySettings = New Laundry
    For Each chk As CheckBox In Me.Controls.OfType(Of CheckBox)
        If chk.Checked Then
            LaundrySettings = CType(LaundrySettings Or CType(chk.Tag, Laundry), Laundry)
        End If
    Next
    Select Case CInt(LaundrySettings)
        Case 1
        'code here
        Case 2
        'code here
        Case 3
        'code here
        'and so on ...
    End Select
End Sub

以防LINQ不是一个选项,这是一个非LINQ版本:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    LaundrySettings = New Laundry
    For Each cntrl As Control In Me.Controls
        If cntrl.Name.StartsWith("chk") Then
            Dim chk As CheckBox = DirectCast(cntrl, CheckBox)
            If chk.Checked Then
                LaundrySettings = CType(LaundrySettings Or CType(cntrl.Tag, Laundry), Laundry)
            End If
        End If
    Next
    Select Case CInt(LaundrySettings)
        Case 1
        'code here
        Case 2
        'code here
        Case 3
            'code here
            'and so on ...
    End Select
End Sub