逐行读取文件的多项选择测验 vb.net [英] Reading to a file line by line multiple choice quiz vb.net
问题描述
我正在尝试创建一个多选测验,它将从文本文件中读取问题和答案,如果用户选择的 RadioButton 包含与答案相同的文本,那么标记将增加 2.我已经尝试了每一个单循环和技术,但它要么只读取最后一行(for 循环),要么根本不读取文本文件(do until/do while/while).我需要帮助.这是我目前所做的
I'm trying to create a multi choice quiz that will read questions and answers from a text file and if the RadioButton selected by the user contains text the same as the answer then the mark will increment by 2. I have tried every single loops and techniques but it either reads the last line only (for loop) or doesn't read the text file at all (do until / do while / while). I need help. This is what I have done so far
Imports System.IO
Public Class Form5
Dim easy As Boolean
Dim medium As Boolean
Dim difficult As Boolean
Dim easytest As New System.IO.StreamReader("he.txt")
Dim lineseasy() As String = IO.File.ReadAllLines("he.txt")
Dim mediumtest As New System.IO.StreamReader("hm.txt")
Dim linesmedium() As String = IO.File.ReadAllLines("hm.txt")
Dim difficulttest As New System.IO.StreamReader("hd.txt")
Dim fulldata As String
Dim mark As Integer = 0
Private Sub Form5_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Label2.Text = Form3.ComboBox1.Text
Label3.Text = Form3.ComboBox2.Text
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
easytest.Read()
Dim counter As Integer = 0
Dim answer As String
While (easytest.Peek() <> -1)
Dim items() As String = lineseasy(1).Split(",")
Label4.Text = items(0)
RadioButton1.Text = items(1)
RadioButton2.Text = items(2)
answer = Str(3)
End While
End Sub
End Class
推荐答案
你为什么需要一个文本文件,如果你使用 Access 数据库或 sql 数据库,它会很简单.例如,您在 sql 或 access 中创建一个数据库,在其中创建一个表,创建 3 列,第一列用于问题 ID,第二列用于问题,最后一列用于答案.现在添加您的问题 ID(任何随机 alpha-数字/任何字符串值),特定单元格中的问题和答案一一.现在在您的 vb.net 应用程序中,添加一个包含问题 ID 的标签(使其不可见),添加对 System.Data.SqlClient(用于 sql 数据库)或 System.Data.OleDb(用于访问)的引用强>.现在使用以下代码检查答案是否正确..
Why do you want a textfile where if u use Access database or sql database, it will be very simple . For example, you create a db in either sql or access, create a table in it, create 3 columns, 1st column for question id,2nd column for question and the last one for the answer.Now add your question id(any random alpha-numeric/any string value),questions and answers in specific cells one by one. Now in you vb.net app,add a label(make it invisible) that will contain the question id, add reference to System.Data.SqlClient(for sql database) or System.Data.OleDb(for access). Now use the following code to check if the answer is correct or not ..
'add this code to all 4 radio buttons
Private Sub RadioBtn1_CheckedChanged
'create your connection string
connectionstring.open
Dim cmd as new SqlCommand("Select * from [table name from database-remove brackets if required] where [question id]=@quid and [answer]=@ans",connectionstring) 'use OleDbCommand of access db
cmd.parametres.add("@quid",sqldbtype.varchar).value=label1.text 'use oledbtype for access db
cmd.parametres.add("@ans",sqldbtype.varcha).value=radiobutton1.text 'when adding this code to radio button2, chage radiobutton1.text with radiobutton2.text , when using it in radio button 3 do the same
Dim adapter As New SqlDataAdapter(cmd) ' use oledbdataadapter for access
Dim table As New DataTable
adapter.Fill(table)
If table.Rows.Count() <= 0 Then
msgbox("WRONG ANSWER")
Else
marktext.text = mark.text+2 ' to increase point by 2
'what ever you want to do when the answer is correct
end if
现在,我希望你知道如何从数据库加载问题/问题 ID(我的意思是数据)...如果你不这样做,请留下回复,我会添加它们
Now, i hope u know how to load the question/question id(i mean the data) from the database...if you don't that please leave a reply and i'll add them
这篇关于逐行读取文件的多项选择测验 vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!