Hi All,

I have noticed that there is another thread about this matter, but could not quite get my head around it.

Basically I have to create an application which is a quiz. According to the users score it will then determine what level of skill they have.

I know i could do it really easily, by having a number of labels on the form which hold the questions. Then underneath have radiobuttons with possible answers. From this then use an IF statement to add a digit into an invisble label which can tally a score of correct answers. Assign each invisible label a variable which then can be summed in another label, from the score then use a SELECT CASE statement to let the user know what level they are at.

But I know that this would mean alot of code and objects on the form. I have read that it would be more efficent to use an XML or TXT document to populate a label and radio buttons, this way it would also be easier to change questions and randomise them.

The only problem is that i dont understand how to attach the XML or TXT file to the form so that it does the population of labels & radiobuttons.

Any help would be much appreciated!

rgds
Gary

Recommended Answers

All 20 Replies

You can still load your questions from a database and make changes to them anytime. Am not saying XML is not good but using VB and a database still works well and this way, your files are safe. Think about what happens if you use a text document and that document is mistakenly deleted.;)

very good point!! Will have a look into the database method now. Thanks for your help

See if this helps for starters.
New Project(2 Buttons, 1 Label, 4 RadioButtons)

Public Class Form1

#Region "===----===----===----===----- DECLARATIONS  -----===----===----===----==="
    Public myCoolQuestionsAndAnswersFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\QandA.txt" '// your File.
    Private arlQandA As New ArrayList '// store all File.Lines in.
    Private iSelectedQuestion, iCorrectAnswer As Integer '// set the CurrentQuestion and the CorrectAnswer indexes.
    Private arT() As String '// TEMP String Array; .Splits the File.Line to get values.
#End Region

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        '// load File.
        If IO.File.Exists(myCoolQuestionsAndAnswersFile) Then
            arlQandA.AddRange(IO.File.ReadAllLines(myCoolQuestionsAndAnswersFile))
            setCoolQuestionAndAnswers(iSelectedQuestion)
        Else
            Button1.Enabled = False : Button2.Enabled = False
        End If
        Button1.Text = ".>Next Q." '// next Question.
        Button2.Text = ".check A." '// check Answer.
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        getNextCoolQuestionAndAnswers(iSelectedQuestion)
    End Sub '// next Question.

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        verifySelectedCoolAnswer()
    End Sub '// check Answer.

#Region "===----===----===----===----- QandA ZONE  -----===----===----===----==="

    Private Sub getNextCoolQuestionAndAnswers(ByVal selQuestionIndex As Integer)
        If Not arlQandA.Count - 1 = selQuestionIndex Then '// check if Not the selectedQuestion is the last Question in the ArrayList.
            selQuestionIndex += 1 '// set the QuestionIndex.
            setCoolQuestionAndAnswers(selQuestionIndex) '// set the Question.
        End If
    End Sub

    Private Sub setCoolQuestionAndAnswers(ByVal selQuestionIndex As Integer)
        arT = arlQandA(selQuestionIndex).ToString.Split(">") '// .Split current File line into Arrays.
        Label1.Text = arT(0) '// set first value.
        RadioButton1.Text = arT(1) '// set second value.
        RadioButton2.Text = arT(2) '// etc..
        RadioButton3.Text = arT(3)
        RadioButton4.Text = arT(4)
        iCorrectAnswer = CInt(arT(5))
    End Sub

    Private Sub verifySelectedCoolAnswer()
        For Each rb In New RadioButton() {RadioButton1, RadioButton2, RadioButton3, RadioButton4} '// loop thru RadioButtons and get the # at end of .Name.
            If rb.Checked Then
                If iCorrectAnswer = CInt(rb.Name.Substring(rb.Name.Length - 1)) Then '// check if Correct Answer is same as # at end of rb.Name.
                    MsgBox("Correct Answer.")
                    Exit Sub '// skip remaining code in Sub.
                End If
            End If
        Next
        MsgBox("Incorrect Answer.")
    End Sub
#End Region

End Class

You will also need a .txt File, with similar content.

What do trees talk about?>life>home>music>driving golf carts>1
Where do programmers find comfort?>in creativity>in design>in depth>in life>4

The # at the end of each line is the correct answer for the question.

sorry for late reply, only just seen it.

Thanks for the hand mate, will have a go at that code now.

Gary

sorry to be a pest but having a few problems with the code.

the following use 'Get' 'Set':

'setCoolQuestionAndAnswers(iSelectedQuestion)'
'getNextCoolQuestionAndAnswers(iSelectedQuestion)'

Is this using the 'Get' 'Set' property? Since visual studio says that its not a supported assignment any more

or

should i set those names as a variable?

Just trying to get my head around the code.

thanks

sorry just realised these 'setCoolQuestionAndAnswers''getNextCoolQuestionAndAnswers' are like functions. Got it all running now but doesnt seem to load the txt file i have created.

hi again, having problems with:

arT = (arlQandA(selQuestionIndex).ToString.Split(">"))

Whilst in debug. it come up with the error:

"System.ArgumentOutOfRangeException"

could i get this problem is 'arlQandA' Count = 0 ?

>>doesnt seem to load the txt file i have created.
Post a few of the file content lines.
>>it come up with the error:
I cannot recreate the error, although it could all be related to your file.content.
>>thanks
:)

thanks for the reply, i have current just made a few questions which are not related to the application i have to write. I basically want to learn the code you have supplied and fully understand it before even attempting to use it properly. The current questions i have got are:

my name?>Gary>Bryn>Kieran>Rae>1#
my age?>22>28>29>30>2#
is my hair colour?>grey>ginger>brown>black>3#

i have changed the file directory address exact to the folder my project is stored in. I created the txt file by 'add new item' within VB.

thanks for your help!

Are you still having issues w/this project?:
>>The current questions i have got are:

Or are you just somewhat spamming your own thread?:D (since no question(s) was/were asked in your previous reply)

Sorted now thanks. Ended up deleting the project and starting from scratch. Thanks for your help, it is much appreciated. I now fully understand the code and can now chnage it to what i need.

Didnt realise the questions hadnt pasted accross.

sorry one last question.

Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\QandA.txt"

This code enables the txt file to be read from the desktop. Now what would change in this so that i can create a txt document as a 'new object' in vb and have it read from the project folder. Think that would be better for compiling since the program needs to be able to run on other workstations and think having the txt file sat on the desktop is a bit risky.

thanks

See if this helps.

Dim myCoolFolder As String = Application.StartupPath & "\"
        Dim myCoolQuestionsAndAnswersFile As String = myCoolFolder & "QandA.txt" '// your File.
        Process.Start(myCoolFolder) '// load Folder.
        '// place file in this Folder while Debugging and place File in same Folder as your app. when installing.

sorted thanks, been looking at how to put it into the resources folder hoping that would sort it out. Used:

Public QandAFile As String = (My.Resources.Resource1.QandA)

no error with code but does not read in the text document.

My.Resources, nice choice.
You do have to read the file from the My.Resources just as you have to read it from a folder.
Hope this helps.:)

tried your method of loading the folder replacing parts for the my.resources but getting an error with 'Process' from 'Process.Start(QandAFile)'. the error is saying declaration expected. Also get the same error when trying the code you supplied. Currently my code is looking like this:

Dim ResourceFolder As String = Application.StartupPath & "My.Resources.Resource1.QandA"

    'Create Sting var with connection to txt file
    Public QandAFile As String = ResourceFolder & "QandA.txt"
    Process.Start(QandAFile)

Appologies for being a pain in the arse! I have read loads of pages now on the resource folder but can only find threads to do with using it for images.

rgds

q.q.(quick question)
Is the file in My.Resources or is it in the Application.StartupPath folder?

it is in the My.Resources folder mate.

researched loads on getting files from the foldder but still cant get my head around it.

thnks

i thought 'Application.StartupPath' was a command for stating the path of the folder.

>>i thought 'Application.StartupPath' was a command for stating the path of the folder.
Hobbyist programmer here, though I think that "My.Resources" gets built into the .exe, and is not an external folder; could be wrong though, long day in the sun around drunks for Octoberfest, should be Octobeerfest:D, though overall, thanx for clearing up the issue and marking the thread as Solved.:) (+=glad.i.could help:))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.