Vb.Net 带输入验证的消息框
Vb.Net Message box with input validation
所以我买了一台新笔记本电脑,为了帮助我做出决定,我想出了一个积分系统。我已经制作了一个应用程序来帮助计算,它由一系列复选框组成,这些复选框具有针对不同类别的组件等(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
结束Class
尝试替换
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。
如果您使用适当的控件,您可以节省大量代码。 RadioButton
s 仅允许在容器中进行单一选择。容器可以是分组框或表单。
使用 NumericUpDown
将确保您获得端口区域的号码。用户可以将任何内容放入 TextBox
.
我在按钮代码中多次使用了GetSelectedRadioButton
。此函数使用一个包含单选按钮的容器和 returns 选定的单选按钮。它使用一点 Linq 魔法来完成此操作,它会检查容器中的每个单选按钮,直到找到 .Checked = True
所在的单选按钮。此单选按钮分配给 rb
并返回给调用代码。如果它没有找到选中的单选按钮,它 returns Nothing
.
如果我们尝试检查单选按钮的 .Name
属性,当它是 Nothing
时,我们会得到可怕的 NRE(空引用异常)所以,我们首先要做的是要做的是检查返回的单选按钮是否 Is Nothing
。如果用户未能做出选择,我们会显示一个消息框并退出子程序。
如果用户做出了选择,我们使用 Select Case
或 If
语句来递增 Total
。
当涉及到端口时,我们通过控件循环获取 Value
属性(Decimal
)。要将其添加到总计,必须先将其转换为 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
所以我买了一台新笔记本电脑,为了帮助我做出决定,我想出了一个积分系统。我已经制作了一个应用程序来帮助计算,它由一系列复选框组成,这些复选框具有针对不同类别的组件等(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
结束Class
尝试替换
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。
如果您使用适当的控件,您可以节省大量代码。 RadioButton
s 仅允许在容器中进行单一选择。容器可以是分组框或表单。
使用 NumericUpDown
将确保您获得端口区域的号码。用户可以将任何内容放入 TextBox
.
我在按钮代码中多次使用了GetSelectedRadioButton
。此函数使用一个包含单选按钮的容器和 returns 选定的单选按钮。它使用一点 Linq 魔法来完成此操作,它会检查容器中的每个单选按钮,直到找到 .Checked = True
所在的单选按钮。此单选按钮分配给 rb
并返回给调用代码。如果它没有找到选中的单选按钮,它 returns Nothing
.
如果我们尝试检查单选按钮的 .Name
属性,当它是 Nothing
时,我们会得到可怕的 NRE(空引用异常)所以,我们首先要做的是要做的是检查返回的单选按钮是否 Is Nothing
。如果用户未能做出选择,我们会显示一个消息框并退出子程序。
如果用户做出了选择,我们使用 Select Case
或 If
语句来递增 Total
。
当涉及到端口时,我们通过控件循环获取 Value
属性(Decimal
)。要将其添加到总计,必须先将其转换为 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