根据不同的复选框组合显示消息

时间:2019-10-25 16:40:03

标签: vb.net if-statement

我们有一系列的10个复选框。我们试图使消息显示出来,具体取决于所选复选框的系列。但是,我们的问题是,按下按钮后,它会显示所有可能的组合。

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If CheckBox1.Checked = True And CheckBox6.Checked = True Then MessageBox.Show("On Campus Student Center")
        If CheckBox1.Checked = True And CheckBox7.Checked = True Then MessageBox.Show("On Campus Marketplace")
        If CheckBox1.Checked = True And CheckBox8.Checked = True Then MessageBox.Show("Around Campus")
        If CheckBox2.Checked = True And CheckBox3.Checked = True And CheckBox9.Checked = True Then MessageBox.Show("2 Mile Restaurant")
        If CheckBox2.Checked = True And CheckBox3.Checked = True And CheckBox10.Checked = True Then MessageBox.Show("2 Mile FF")
        If CheckBox2.Checked = True And CheckBox4.Checked = True And CheckBox9.Checked = True Then MessageBox.Show("3 Mile Restaurant")
        If CheckBox2.Checked = True And CheckBox4.Checked = True And CheckBox10.Checked = True Then MessageBox.Show("3 Mile FF")
        If CheckBox2.Checked = True And CheckBox5.Checked = True And CheckBox9.Checked = True Then MessageBox.Show("4 Mile Restaurant")
        If CheckBox2.Checked = True And CheckBox5.Checked = True And CheckBox10.Checked = True Then MessageBox.Show("4 Mile FF")
        If CheckBox1.Checked = True And CheckBox6.Checked = True And CheckBox7.Checked = True And CheckBox8.Checked = True Then MessageBox.Show("All On Campus")
        If CheckBox1.Checked = True And CheckBox6.Checked = True And CheckBox7.Checked = True Then MessageBox.Show("Student Center and Marketplace")
        If CheckBox1.Checked = True And CheckBox6.Checked = True And CheckBox8.Checked = True Then MessageBox.Show("Student Center and Around Campus")
        If CheckBox1.Checked = True And CheckBox8.Checked = True And CheckBox7.Checked = True Then MessageBox.Show("Market Place and around")
    End Sub
End Class

例如,如果我们遵循最后一条语句的复选框选择。复选框1、7和8。我们将看到以下消息框:

  • “在校园市场上”
  • “校园周边”
  • “市场及其周围地区”

我们只希望它显示最终if语句的文本,而不是该系列复选框选择中的所有编码组合。

2 个答案:

答案 0 :(得分:3)

您可以轻松地在Dictionary(Of CheckBox(),String)中设置所有可能的组合,这也将有助于管理它们,因为看起来您有一个重复项(CheckBox 1、6和7)。该键将代表所有需要检查的复选框控件,并且值将是您的消息。

然后在您的按钮单击事件中,将获得字典中的所有项目,在该项目中将检查键中的所有Checkbox控件。然后按键的长度对集合进行排序,并获得第一条消息:

Private ReadOnly combinations As Dictionary(Of CheckBox(), String) = New Dictionary(Of CheckBox(), String)() From {
    { {CheckBox1, CheckBox6}, "On Campus Student Center" },
    { {CheckBox1, CheckBox7}, "On Campus Market Place" },
    { {CheckBox1, CheckBox8}, "Around Campus " },
    { {CheckBox2, CheckBox3, CheckBox9}, "2 Mile Restaurant" },
    { {CheckBox2, CheckBox3, CheckBox10}, "2 Mile FF" },
    { {CheckBox2, CheckBox4, CheckBox9}, "3 Mile Restaurant" },
    { {CheckBox2, CheckBox4, CheckBox10}, "3 Mile FF" },
    { {CheckBox2, CheckBox5, CheckBox9}, "4 Mile Restaurant" },
    { {CheckBox2, CheckBox5, CheckBox10}, "4 Mile FF" },
    { {CheckBox1, CheckBox6, CheckBox7}, "All On Campus " },
    { {CheckBox1, CheckBox6, CheckBox7}, "Student Center and Marketplace " },
    { {CheckBox1, CheckBox6, CheckBox8}, "Student Center and Around Campus" },
    { {CheckBox1, CheckBox8, CheckBox7}, "Marketplace and Around" }
}

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim matchedCombinations = combinations.Where(Function(kvp) kvp.Key.All(Function(cb) cb.Checked))

    If (matchedCombinations.Any()) Then
        Dim bestMatch = matchedCombinations.OrderByDescending(Function(kvp) kvp.Key.Length).First()

        MessageBox.Show(bestMatch.Value, "Valid Combination", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Else
        MessageBox.Show("None of the checked CheckBoxes matched a specified combination", "Invalid Combo", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
End Sub

答案 1 :(得分:1)

如果您不想像其他答案一样使用LINQ,这是一个更简单的答案。从性能的角度来看,它们基本上是相等的,因为您并没有真正遍历很多对象。这基本上是一个优先事项。

HTML

<div>

            <input type="checkbox" id="CheckBox1" runat="server" />
            <input type="checkbox" id="CheckBox2" runat="server" />
            <input type="checkbox" id="CheckBox3" runat="server" />
            <input type="checkbox" id="CheckBox4" runat="server" />
            <input type="checkbox" id="CheckBox5" runat="server" />
            <input type="checkbox" id="CheckBox6" runat="server" />
            <input type="checkbox" id="CheckBox7" runat="server" />
            <input type="checkbox" id="CheckBox8" runat="server" />

            <asp:Button ID="Button1" runat="server" Text="Process" />

        </div>

隐藏代码

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        If CheckBox1.Checked Then

            If CheckBox6.Checked Then
                MessageBox.Show("On Campus Student Center")

            ElseIf CheckBox7.Checked Then
                MessageBox.Show("On Campus Marketplace")

            End If

        ElseIf CheckBox2.Checked Then

            If CheckBox6.Checked Then
                ' message
            ElseIf CheckBox7.Checked Then
                ' message
            End If

        End If

    End Sub