我如何确保用户不会留下任何未选中的多项选择输入?
How can I ensure that a user does not leave any multiple choice inputs unselected?
我正在尝试做一个酒店计费系统,其中用户不能不选择组合框、单选按钮和列表框,我不知道有人可以帮我解决这个问题吗?
-新手
调暗输入为整数 = 0
If RadioButton1.Checked Or RadioButton2.Checked Or RadioButton3.Checked = False Then
inp = 1
End If
If ComboBox1.Text = "" Then
inp = 1
End If
If ListBox1.SelectedIndex() Then
inp = 1
End If
If inp = 1 Then
MessageBox.Show("No selected type of payment" & vbCrLf & "Or" & vbCrLf & "No selected room capacity" & vbCrLf & "Or" & vbCrLf & "No selected room type", "Error")
inp = 0
End If
检查您的代码:
RadioButton2.Checked Or RadioButton2.Checked = False
将始终导致 inp=1
更新
If RadioButton1.Checked = False Or RadioButton2.Checked = False Or RadioButton3.Checked = False Then
inp=1
End If
If ComboBox1.Text = "" Then
inp=1
End If
If ListBox.SelectedItem = -1 Then
inp = 1
End If
If inp = 1 Then
MessageBox.Show("No selected type of payment" & vbCrLf & "Or" & vbCrLf & "No selected room capacity" & vbCrLf & "Or" & vbCrLf & "No selected room type", "Error")
inp = 0
RadioButton1.Checked = False
RadioButton2.Checked = False
RadioButton3.Checked = False
ListBox1.ClearSelected()
ComboBox1.Text = ""
End If
您可以在一个 if 语句中完成所有这些,您只需要检查每个语句的值。
If (RadioButton1.Checked = False AndAlso RadioButton2.Checked = False AndAlso RadioButton3.Checked = False) OrElse Combobox1.SelectedIndex = -1 OrElse ListBox1.SelectedIndex = -1 Then
MessageBox.Show("No selected type of payment" & vbCrLf & "Or" & vbCrLf & "No selected room capacity" & vbCrLf & "Or" & vbCrLf & "No selected room type", "Error")
Exit Sub
End If
一旦发现任何条件为真,将继续使用 OrElse。如果发生这种情况,我们可以立即进入 If 块并显示消息。在这一点上,继续其余的例程没有意义,所以我们退出子程序,允许用户修复他们丢失的信息并重试。
也就是说,您的原始代码中存在多个问题。一个是你的第一行没有按照你认为的那样做。 (根据它的值检查每个条件)。另一个是您没有根据任何内容检查列表框的 selectedindex。
查找布尔值,因为您可以使用它而不是 int 来处理您的 inp 标志。
你也有 inp = inp = 1 我认为你想从中减去 1,在这种情况下,你可以将它设置回 0(见我的布尔评论,它会处理这个允许您将其设置为 true 或 false)
另外,作为个人小费。如果用户犯了错误,没有输入所有预期的内容,请不要养成为了解决问题而清除所有工作的习惯。您的代码块告诉用户他们搞砸了,然后您将所有内容设置回空白值。
我正在尝试做一个酒店计费系统,其中用户不能不选择组合框、单选按钮和列表框,我不知道有人可以帮我解决这个问题吗?
-新手
调暗输入为整数 = 0
If RadioButton1.Checked Or RadioButton2.Checked Or RadioButton3.Checked = False Then
inp = 1
End If
If ComboBox1.Text = "" Then
inp = 1
End If
If ListBox1.SelectedIndex() Then
inp = 1
End If
If inp = 1 Then
MessageBox.Show("No selected type of payment" & vbCrLf & "Or" & vbCrLf & "No selected room capacity" & vbCrLf & "Or" & vbCrLf & "No selected room type", "Error")
inp = 0
End If
检查您的代码:
RadioButton2.Checked Or RadioButton2.Checked = False
将始终导致 inp=1
更新
If RadioButton1.Checked = False Or RadioButton2.Checked = False Or RadioButton3.Checked = False Then
inp=1
End If
If ComboBox1.Text = "" Then
inp=1
End If
If ListBox.SelectedItem = -1 Then
inp = 1
End If
If inp = 1 Then
MessageBox.Show("No selected type of payment" & vbCrLf & "Or" & vbCrLf & "No selected room capacity" & vbCrLf & "Or" & vbCrLf & "No selected room type", "Error")
inp = 0
RadioButton1.Checked = False
RadioButton2.Checked = False
RadioButton3.Checked = False
ListBox1.ClearSelected()
ComboBox1.Text = ""
End If
您可以在一个 if 语句中完成所有这些,您只需要检查每个语句的值。
If (RadioButton1.Checked = False AndAlso RadioButton2.Checked = False AndAlso RadioButton3.Checked = False) OrElse Combobox1.SelectedIndex = -1 OrElse ListBox1.SelectedIndex = -1 Then
MessageBox.Show("No selected type of payment" & vbCrLf & "Or" & vbCrLf & "No selected room capacity" & vbCrLf & "Or" & vbCrLf & "No selected room type", "Error")
Exit Sub
End If
一旦发现任何条件为真,将继续使用 OrElse。如果发生这种情况,我们可以立即进入 If 块并显示消息。在这一点上,继续其余的例程没有意义,所以我们退出子程序,允许用户修复他们丢失的信息并重试。
也就是说,您的原始代码中存在多个问题。一个是你的第一行没有按照你认为的那样做。 (根据它的值检查每个条件)。另一个是您没有根据任何内容检查列表框的 selectedindex。
查找布尔值,因为您可以使用它而不是 int 来处理您的 inp 标志。
你也有 inp = inp = 1 我认为你想从中减去 1,在这种情况下,你可以将它设置回 0(见我的布尔评论,它会处理这个允许您将其设置为 true 或 false)
另外,作为个人小费。如果用户犯了错误,没有输入所有预期的内容,请不要养成为了解决问题而清除所有工作的习惯。您的代码块告诉用户他们搞砸了,然后您将所有内容设置回空白值。