Public Class Form1
'stores the questions
Dim questions As New ArrayList
'represents one question and a set of answers
Structure qa
Dim question As String
Dim answers As Dictionary(Of String, Boolean)
End Structure
'if the answer is selected
Dim selectedIsAnswer As Boolean = False
'when the next button is clicked
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
If selectedIsAnswer Then
MessageBox.Show("Correct")
'get the next question if it is correct
getQuestion()
Else
MessageBox.Show("Incorrect")
End If
End Sub
'on load create questions
'you can load from an sql server/file/xml/etc..
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'create questions
Dim qa1 As qa
qa1.question = "Which of the following is not a data type?"
qa1.answers = New Dictionary(Of String, Boolean)
qa1.answers.Add("String", False)
qa1.answers.Add("Integer", False)
qa1.answers.Add("Boolean", False)
qa1.answers.Add("Car", True)
Dim qa2 As qa
qa2.question = "What is the decimal equavilant of 1010?"
qa2.answers = New Dictionary(Of String, Boolean)
qa2.answers.Add("1", False)
qa2.answers.Add("23", False)
qa2.answers.Add("20", False)
qa2.answers.Add("10", True)
'add to arraylist
questions.Add(qa1)
questions.Add(qa2)
'load questions into panel
getQuestion()
End Sub
Private Sub getQuestion()
If questions.Count > 0 Then
'clear controls in panel and add new question set
pnQuestions.Controls.Clear()
selectedIsAnswer = False
createQuestion(questions.Item(0))
'remove question from arraylist
questions.RemoveAt(0)
Else
pnQuestions.Controls.Clear()
MessageBox.Show("done")
End If
End Sub
Private Sub createQuestion(ByVal qnaInfo As qa)
Dim lblQuestion As New Label
lblQuestion.Text = qnaInfo.question
'sets the location depending on the size of the panel and the height based on the total characters in the question
lblQuestion.SetBounds(0, 0, pnQuestions.Width - 4, CInt(Math.Ceiling(qnaInfo.question.Length / 75) * 14))
pnQuestions.Controls.Add(lblQuestion)
'adds each choice
Dim yLocation As Integer = lblQuestion.Height + 16
For Each answer As String In qnaInfo.answers.Keys
Dim choice As New qaRad
choice.Text = answer
choice.isAnswer = qnaInfo.answers.Item(answer)
'set its location relative to controls next to it and its text size
choice.SetBounds(25, yLocation, pnQuestions.Width - 16, CInt(Math.Ceiling(answer.Length / 75) * 16))
AddHandler choice.CheckedChanged, AddressOf setAnswer
pnQuestions.Controls.Add(choice)
yLocation += choice.Height + 4
Next
End Sub
'subroutine to be called each time a radiobutton is checked
Private Sub setAnswer(ByVal sender As Object, ByVal e As EventArgs)
''you can add the answer to a scoreboard or something...
Dim rad As qaRad = sender
selectedIsAnswer = rad.isAnswer
End Sub
'inherited radio button just to store an extra variable
Public Class qaRad
Inherits RadioButton
Protected Friend isAnswer As Boolean = False
End Class
End Class