【发布时间】:2019-04-03 01:53:08
【问题描述】:
如果用户想返回上一个问题,我想知道系统如何识别问题是否已经回答。如果它已经回答,答案将更新分数是否增加,或者如果答案未编辑,它将是相同的。
BUTTON1 是返回上一个问题 Button2 用于检查答案的真假 按钮 3 用于继续考试
这是我的代码 sn-ps:
Imports MySql.Data.MySqlClient
Imports System.Drawing
Imports System.IO
Public Class Exam
'declaring variables for connection'
Dim score As Integer
Dim rightans As String
Dim correct As Integer = 0
Dim choice As String
Dim choice1 As String
Dim choice2 As String
Dim choice3 As String
Dim con As MySqlConnection
Dim con1 As MySqlConnection
Dim COMMAND As MySqlCommand
Dim read As MySqlDataReader
Dim da As MySqlDataAdapter
Dim sql As String
Private Sub Exam_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'setting the radiobutton to false, so that when the form load there is no shaded button'
Label2.Text = 1
A.Checked = False
B.Checked = False
C.Checked = False
D.Checked = False
con = New MySqlConnection
Button1.Enabled = False
con.ConnectionString = "server=localhost;userid=root;password=;database=user;"
'calling sub
qno()
End Sub
Sub clear()
'to set the radiobutton false/no shaded.
A.Checked = False
B.Checked = False
C.Checked = False
D.Checked = False
End Sub
Sub qno()
'calling connection'
Try
con = New MySqlConnection
con.ConnectionString = "server=localhost; user id=root; password=; database=user;"
con.Open()
sql = "SELECT * FROM user.math WHERE question_id = @ID;"
COMMAND = New MySqlCommand
With COMMAND
.Connection = con
.CommandText = sql
.Parameters.Clear()
.Parameters.AddWithValue("@ID", Label2.Text)
.ExecuteNonQuery()
End With
Dim arrImage() As Byte
Dim dt As New DataTable
da = New MySqlDataAdapter
da.SelectCommand = COMMAND
da.Fill(dt)
If dt.Rows.Count > 0 Then
arrImage = dt.Rows(0).Item(7)
Dim mstream As New System.IO.MemoryStream(arrImage)
Pic1.Image = Image.FromStream(mstream)
question.Text = dt.Rows(0).Item(1)
A.Text = dt.Rows(0).Item(2)
B.Text = dt.Rows(0).Item(3)
C.Text = dt.Rows(0).Item(4)
D.Text = dt.Rows(0).Item(5)
Else
MsgBox("No results!")
End If
Catch ex As MySqlException
MsgBox(ex.Message)
Finally
con.Close()
da.Dispose()
End Try
End Sub
Sub increment()
'incrementing the score f the answer is correct'
Dim i As Integer = 0
i = Label2.Text
i = i + 1
Label2.Text = i
If Label2.Text > 1 Then
Button1.Enabled = True
End If
End Sub
Sub decrement()
'incrementing the score f the answer is correct'
Dim i As Integer = 1
i = Label2.Text
i = i - 1
Label2.Text = i
If Label2.Text = 1 Then
Button1.Enabled = False
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'checking of the user answer the questions'
If ((A.Checked = False) And (B.Checked = False) And (C.Checked = False) And (D.Checked = False)) Then
MsgBox("Please answer the question")
Else
'if the examinee answers all the examination it will call another questions from database'
If A.Checked = True Then
con.Open()
' Dim ans As String
Dim arren As String = "A"
Dim sql As String = ("select answer from user.math where question_id = '" & Label2.Text & "' ")
COMMAND = New MySqlCommand(sql, con)
Dim it As String
read = COMMAND.ExecuteReader
If read.HasRows Then
If read.Read Then
it = read.Item("answer")
If it = choice Then
correct = correct + 1
Label4.Text = correct
ElseIf it <> choice And Label2.Text <= 1 Then
correct = correct - 1
Label4.Text = correct
End If
End If
clear()
End If
If Label2.Text = 10 Then
MessageBox.Show("proceed to other subject test")
End If
con.Close()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
decrement()
qno()
End Sub
End Class
【问题讨论】:
-
那里的代码太多了,你的问题也太模糊了。您需要对问题非常具体,并且只发布与该问题相关的代码。如果您基本上是在说“这就是我所做的,其余的我该怎么做”,那么这不是我们真正要回答的。无论你想做什么,你都需要先尝试去做,然后你可以问为什么它不起作用。然后,您将只发布与该问题相关的代码。
-
开启 Option Strict。这将帮助您避免运行时错误。
-
如果您将按钮命名为有意义的名称,将会很有帮助。 Button2 看起来像 Accept Answer 但 Button1 和 Button3 没有多大意义。
-
您正在检查 Button2 中检查了哪些单选按钮,那么为什么所有这些检查更改的事件?
-
去掉 Sub qno 中的
.ExecuteNonQuery()。选择语句是返回行的查询,您可以使用da.Fill(dt)执行命令
标签: mysql vb.net loops xampp system