【问题标题】:VB.NET How to display a messagebox if no radio button selected and clear them when next button clickedVB.NET 如果没有选择单选按钮,如何显示消息框并在单击下一个按钮时清除它们
【发布时间】:2023-03-21 12:31:01
【问题描述】:

您好,我正在创建一个考试系统作为学校项目。我正在使用 Visual Studio 2010,后端是 MS Access 2007。我在表单上有 4 个单选按钮和 1 个按钮命令(NEXT)。

如果取消选择所有 4 个单选按钮,如何显示消息框?此外,如果选择了 4 个单选按钮之一,则应将答案插入数据库,当单击下一个按钮继续下一个问题时,应清除单选按钮。

我当前的代码如下所示:如果我检查了radiobtnA,答案 A 将被插入到数据库中,当我单击下一步按钮时,单选按钮将全部清除。

下面是我的代码:

Private Sub nxtBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nxtBTN.Click Dim EQcorrAnswer As Integer = 0 Dim strAnswer As String = "" If radiobtnA.Checked = True Then strAnswer = "A" radiobtnA.Checked = False If radiobtnB.Checked = True Then strAnswer = "B" radiobtnB.Checked = False If radiobtnC.Checked = True Then strAnswer = "C" radiobtnC.Checked = False If radiobtnD.Checked = True Then strAnswer = "D" radiobtnD.Checked = False If corrAnsTB.Text = strAnswer Then EQcorrAnswer = 1 intcorrAnswer += 1 End If ExecNonQuery("UPDATE Examinee SET score = " & intcorrAnswer & " AND timefin ='" & timetxt.Text & "' AND datetaken ='" & TXTDate.Text & "' Where tempID = '" & tempIDTB.Text & "'") Try ExecNonQuery("INSERT INTO ExamResult (tempID,QuestionID,answer,correct) VALUES('" & tempIDTB.Text & "','" & questIDTB.Text & "','" & strAnswer & "'," & EQcorrAnswer & ")") If inc <> maxrows - 1 Then inc = inc + 1 navigaterecords()

【问题讨论】:

  • 我真的不能说,我只能假设,但我认为你对 SQL 注入持开放态度。它不会解决问题,但您应该考虑 SQL 参数而不是将字符串连接在一起。

标签: vb.net


【解决方案1】:

我有一些东西可以让你更轻松,而不是使用那富有洞察力的 If [...] 声明!

我的建议是首先添加一个事件处理程序来处理所有 RadioButtons 的“CheckChanged”事件:

Dim SelectedAnswer As RadioButton = Nothing
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButtonA.CheckedChanged, RadioButtonB.CheckedChanged, RadioButtonD.CheckedChanged, RadioButtonC.CheckedChanged

End Sub

并且还将以下内容添加到上面的 Sub. 这将过滤事件,因此它只关注涉及被选中的 RadioButtons 之一的事件,而不是 UnChecked。

    If DirectCast(sender, RadioButton).Checked Then
        SelectedAnswer = DirectCast(sender, RadioButton)
        strAnswer = SelectedAnswer.Name.Substring(8, 1)
        'And potentially anything else you wanted to add could go here
    Else
        SelectedAnswer = Nothing
        'Or here
    End If

这个(上面)正在做什么(非常彻底)是使用“发送者”,这将是触发事件的对象。由于我们有 4 个可能的对象,因此我们使用 DirectCast() 将它提供给我们的通用对象类型转换回它的实际状态(单选按钮)。

TryCast() 也可以使用,它可以用于检查它是否成功地转换(或转换)了类型,只需检查以确保它不是Nothing。它使用的参数与我将在下面为 DirectCast() 解释的参数相同。

因此,对于第一个参数,我们输入 Object 'sender',而对于第二个参数,我们使用要用于将 'sender' 转换为的对象的类型。所以我们使用 RadioButton。

由于我们不希望任何其他类型触发此事件,我们可以毫无问题地使用 DirectCast()。


现在为您提供的按钮单击事件:

Dim strAnswer As String = ""
Private Sub nxtBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nxtBTN.Click
    If SelectedAnswer = Nothing Then
        MessageBox.Show("Nothing selected, silly :p")
        Exit Sub
    End If
    Dim EQcorrAnswer As Integer = 0
    SelectedAnswer.Checked = False
    SelectedAnswer = Nothing

    If corrAnsTB.Text = strAnswer Then
        EQcorrAnswer = 1
        intcorrAnswer += 1
    End If
    ...
    ...
End Sub

我使用If [...] 来检查是否实际选择了答案。 如果没有,显示消息并退出 Sub 以阻止它继续前进。

然后如果 Sub 没有退出,将选中的 RadioButton (SelectedAnswer) 更改为未选中,如果我们不再需要它,则重置该变量。


然后剩下的代码(我没有包含在最后一个片段中)将代替...'s。


希望我能帮到你,并且解释得很好! 当然这是一个建议,所以你可以随心所欲。

【讨论】:

    猜你喜欢
    • 2020-11-05
    • 1970-01-01
    • 2011-02-10
    • 2013-01-13
    • 2012-09-26
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    相关资源
    最近更新 更多