逐行读取文件行多选quiz vb.net

时间:2017-11-21 20:18:26

标签: vb.net

我正在尝试创建一个多选题测验,它将从文本文件中读取问题和答案,如果用户选择的RadioButton包含与答案相同的文本,那么标记将增加2.我已尝试过每一个单循环和技术,但它只读取最后一行(for循环)或根本不读取文本文件(直到/ do while / while)。我需要帮助。 这就是我到目前为止所做的事情

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

1 个答案:

答案 0 :(得分:0)

为什么你想要一个文本文件,如果你使用Access数据库或sql数据库,它将非常简单。 例如,你在sql或access中创建一个db,在其中创建一个表,创建3列,第一列为问题id,第二列为问题,最后一列为answer.Now添加你的问题id(任何随机alpha -numeric /任何字符串值),逐个特定单元格中的问题和答案。现在在你的vb.net app中,添加一个标签(让它不可见),它将包含问题id,添加对 System.Data.SqlClient(对于sql数据库)或System.Data.OleDb(对于访问)的引用< / strong>即可。现在使用以下代码检查答案是否正确..

   '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(我的意思是数据)......如果你不这样做,请留下回复,我会添加它们