Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

Reading to a file line by line multiple choice quiz vb.net

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...