0

所以我买了一台新的笔记本电脑,并帮助我决定我想出了一个积分系统。我制作了一个应用程序来帮助计算,它由一系列复选框组成,这些复选框具有不同类别的组件等(CPU、RAM 等)的不同选项。他们根据他们的表现获得积分。我有几行代码可以检查每个类别是否选择了多个组件。如果超过 1 个,则会弹出一个消息框,告诉用户然后重置选项。但是,第一次执行后,消息框会出现两次。

错误视频:https ://imgur.com/a/D9WSVO5

这是我的第一个 vb.net 独奏 GUI 程序,所以请善待。

非常感谢, :)

这是程序界面

Public Class Form1
Dim IS_R54500 As Boolean = False
Dim IS_R53500 As Boolean = False
Dim IS_R34300 As Boolean = False
Dim IS_Gb4 As Boolean = False
Dim IS_Gb8 As Boolean = False
Dim IS_Screen14 As Boolean = False
Dim IS_Screen15 As Boolean = False
Dim IS_SSD128 As Boolean = False
Dim IS_SSD256 As Boolean = False
Dim IS_SSD512 As Boolean = False
Dim IS_USBA As Integer = 0
Dim IS_USBC As Integer = 0
Dim IS_HDMI As Integer = 0
Dim IS_DP As Integer = 0
Dim Total As Integer = 0

Public Sub R54500_CheckedChanged(sender As Object, e As EventArgs) Handles R54500.CheckedChanged
    IS_R54500 = True
End Sub

Public Sub R53500_CheckedChanged(sender As Object, e As EventArgs) Handles R53500.CheckedChanged
    IS_R53500 = True
End Sub

Public Sub R34300_CheckedChanged(sender As Object, e As EventArgs) Handles R34300.CheckedChanged
    IS_R34300 = True
End Sub

Public Sub Gb4_CheckedChanged(sender As Object, e As EventArgs) Handles Gb4.CheckedChanged
    IS_Gb4 = True
End Sub

Public Sub Gb8_CheckedChanged(sender As Object, e As EventArgs) Handles Gb8.CheckedChanged
    IS_Gb8 = True
End Sub

Public Sub Screen14_CheckedChanged(sender As Object, e As EventArgs) Handles Screen14.CheckedChanged
    IS_Screen14 = True
End Sub

Public Sub Screen15_CheckedChanged(sender As Object, e As EventArgs) Handles Screen15.CheckedChanged
    IS_Screen15 = True
End Sub

Public Sub SSD128_CheckedChanged(sender As Object, e As EventArgs) Handles SSD128.CheckedChanged
    IS_SSD128 = True
End Sub

Public Sub SSD256_CheckedChanged(sender As Object, e As EventArgs) Handles SSD256.CheckedChanged
    IS_SSD256 = True
End Sub

Public Sub SSD512_CheckedChanged(sender As Object, e As EventArgs) Handles SSD512.CheckedChanged
    IS_SSD512 = True
End Sub

Public Sub USBA_TextChanged(sender As Object, e As EventArgs) Handles USBA.TextChanged
    IS_USBA = USBA.Text()
End Sub

Public Sub USBC_TextChanged(sender As Object, e As EventArgs) Handles USBC.TextChanged
    IS_USBC = USBC.Text()
End Sub

Public Sub HDMI_TextChanged(sender As Object, e As EventArgs) Handles HDMI.TextChanged
    IS_HDMI = HDMI.Text()
End Sub

Public Sub DP_TextChanged(sender As Object, e As EventArgs) Handles DP.TextChanged
    IS_DP = DP.Text()
End Sub

Public Sub Results_Click(sender As Object, e As EventArgs) Handles Results.Click
    Results.Text = CStr(Total)
End Sub

Public Sub Calculate_Click(sender As Object, e As EventArgs) Handles Calculate.Click
    Total = 0

    If IS_R54500 And IS_R53500 = True Then
        MessageBox.Show("There is more than one option chosen in a certain category." & vbCrLf & "Please fix this error and then try again.", "Error")
        IS_R54500 = False
        IS_R53500 = False
        R54500.CheckState = CheckState.Unchecked
        R53500.CheckState = CheckState.Unchecked
    End If
    If IS_R54500 And IS_R34300 = True Then
        MessageBox.Show("There is more than one option chosen in a certain category." & vbCrLf & "Please fix this error and then try again.", "Error")
        IS_R54500 = False
        IS_R34300 = False
        R54500.CheckState = CheckState.Unchecked
        R34300.CheckState = CheckState.Unchecked
    End If
    If IS_R53500 And IS_R34300 = True Then
        MessageBox.Show("There is more than one option chosen in a certain category." & vbCrLf & "Please fix this error and then try again.", "Error")
        IS_R53500 = False
        IS_R34300 = False
        R53500.CheckState = CheckState.Unchecked
        R34300.CheckState = CheckState.Unchecked
    End If
    If IS_Gb8 And IS_Gb4 = True Then
        MessageBox.Show("There is more than one option chosen in a certain category." & vbCrLf & "Please fix this error and then try again.", "Error")
        IS_Gb4 = False
        IS_Gb8 = False
        Gb4.CheckState = CheckState.Unchecked
        Gb8.CheckState = CheckState.Unchecked
    End If
    If IS_Screen14 And IS_Screen15 = True Then
        MessageBox.Show("There is more than one option chosen in a certain category." & vbCrLf & "Please fix this error and then try again.", "Error")
        IS_Screen14 = False
        IS_Screen15 = False
        Screen14.CheckState = CheckState.Unchecked
        Screen15.CheckState = CheckState.Unchecked
    End If
    If IS_SSD128 And IS_SSD256 = True Then
        MessageBox.Show("There is more than one option chosen in a certain category." & vbCrLf & "Please fix this error and then try again.", "Error")
        IS_SSD128 = False
        IS_SSD256 = False
        SSD128.CheckState = CheckState.Unchecked
        SSD256.CheckState = CheckState.Unchecked
    End If
    If IS_SSD128 And IS_SSD512 = True Then
        MessageBox.Show("There is more than one option chosen in a certain category." & vbCrLf & "Please fix this error and then try again.", "Error")
        IS_SSD128 = False
        IS_SSD512 = False
        SSD128.CheckState = CheckState.Unchecked
        SSD512.CheckState = CheckState.Unchecked
    End If
    If IS_SSD512 And IS_SSD256 = True Then
        MessageBox.Show("There is more than one option chosen in a certain category." & vbCrLf & "Please fix this error and then try again.", "Error")
        IS_SSD512 = False
        IS_SSD256 = False
        SSD512.CheckState = CheckState.Unchecked
        SSD256.CheckState = CheckState.Unchecked
    End If

    If IS_R54500 = True Then
        Total += 3
    End If
    If IS_R53500 = True Then
        Total += 2
    End If
    If IS_R34300 = True Then
        Total += 1
    End If
    If IS_Gb8 = True Then
        Total += 2
    End If
    If IS_Gb4 = True Then
        Total += 1
    End If
    If IS_Screen14 = True Then
        Total += 1
    End If
    If IS_Screen15 = True Then
        Total += 2
    End If
    If IS_SSD128 = True Then
        Total += 1
    End If
    If IS_SSD256 = True Then
        Total += 2
    End If
    If IS_SSD512 = True Then
        Total += 3
    End If
    Total += CInt(IS_USBA)
    Total += CInt(IS_USBC)
    Total += CInt(IS_HDMI)
    Total += CInt(IS_DP)

    ''MessageBox.Show("There is more than one option chosen in a certain category." & vbCrLf & "Please fix this error and then try again.", "Error")

    Results.Text = Total
End Sub

结束类

4

2 回答 2

0

如果您使用适当的控件,您可以为自己节省大量代码。RadioButtons 只允许在容器中进行单个选择。容器可以是组框或表单。

使用 a NumericUpDownwill 确保您获得 Ports 区域的号码。用户可以在TextBox.

GetSelectedRadioButton在按钮代码中使用了几次。此函数接受一个包含单选按钮的容器并返回选定的单选按钮。它使用一点 Linq 魔法来做到这一点,它检查容器中的每个单选按钮,直到找到一个 where .Checked = True。此单选按钮分配给rb调用代码并返回给调用代码。如果它没有找到选中的单选按钮,则返回Nothing.

如果我们尝试检查.Name单选按钮的属性,Nothing我们会得到可怕的 NRE(空引用异常)所以,我们要做的第一件事就是检查是否返回单选按钮Is Nothing。如果用户未能做出选择,我们会显示一个消息框并退出子程序。

如果用户做出了选择,我们使用 a Select CaseorIf语句来增加Total

当涉及到端口时,我们循环通过控件获取Value属性 (a Decimal)。要将其添加到 Total,必须首先将其转换为Integer.

最后显示总计。

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim Total As Integer
    Dim rb = GetSelectedRadioButton(gbProcessor)
    If rb Is Nothing Then
        MessageBox.Show("Please select a Processor.")
        Exit Sub
    Else
        Select Case GetSelectedRadioButton(gbProcessor).Name
            Case "rb545"
                Total += 3
            Case "rb535"
                Total += 2
            Case "rb343"
                Total += 1
        End Select
    End If
    Dim rb1 = GetSelectedRadioButton(gbMemory)
    If rb1 Is Nothing Then
        MessageBox.Show("Please select Memory")
        Exit Sub
    Else
        If rb1.Name = "rb4" Then
            Total += 1
        Else
            Total += 2
        End If
    End If
    Dim rb2 = GetSelectedRadioButton(gbScreenSize)
    If rb2 Is Nothing Then
        MessageBox.Show("Please select a Screen Size.")
        Exit Sub
    Else
        If rb2.Name = "rb14" Then
            Total += 1
        Else
            Total += 2
        End If
    End If
    Dim rb3 = GetSelectedRadioButton(gbStorage)
    If rb3 Is Nothing Then
        MessageBox.Show("Please select Storage size.")
        Exit Sub
    Else
        Select Case rb3.Name
            Case "rb128"
                Total += 1
            Case "rb256"
                Total += 2
            Case "rb512"
                Total += 3
        End Select
    End If
    For Each ctrl As NumericUpDown In gbPorts.Controls.OfType(Of NumericUpDown)
        Total += CInt(ctrl.Value)
    Next
    lblCalculate.Text = Total.ToString
End Sub

Private Function GetSelectedRadioButton(Container As Control) As RadioButton
    Dim rb = Container.Controls.OfType(Of RadioButton)().FirstOrDefault(Function(r) r.Checked = True)
    Return rb
End Function
于 2021-04-09T06:28:47.607 回答
0

尝试更换

Public Sub SSD512_CheckedChanged(sender As Object, e As EventArgs) Handles SSD512.CheckedChanged
    IS_SSD512 = True
End Sub

Public Sub SSD512_CheckedChanged(sender As Object, e As EventArgs) Handles SSD512.CheckedChanged
    IS_SSD512 = SSD512.Checked
End Sub

我认为问题是当表单加载时,它会调用取消选中这些框,因此您的变量变为 TRUE。

于 2021-04-08T23:14:57.433 回答