Luigi Nesbitt
New member
- Joined
- Nov 29, 2017
- Messages
- 1
- Programming Experience
- 1-3
I would like to put this little piece of code out there for as my title says: Feedback, Suggestions, Improvement Ideas if anyone is willing to look my code over! Thanks!
I put some questions in the comments of the code also.
Thanks for any input, its a project for my church quizzing team!
VB.NET:
Option Strict On
Option Explicit On
Imports System.IO
Public Class Main
Public Class QuestionWithAnswers
Public Property ItemNumber As Integer
Public Property Question As String
Public Property Answers As New List(Of Answer)
Public Property CorrectAnswer As String
Public Class Answer
Public AnswerText As String
Public AnswerChecked As Boolean
End Class
End Class
Private AllQuestions As New List(Of QuestionWithAnswers)
Private qIndex As Integer = 0
Private updatingQuestion As Boolean
Private curQandA As QuestionWithAnswers
Private _myIndex As Integer = 1
Private Sub BtnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
If AllQuestions.Count = 0 Then Exit Sub
curQandA = AllQuestions.ElementAt(qIndex)
If qIndex < 9 Then ' <-----'Is it safe to Increment (also: how to change the qIndex integer up or down with button control if you want more than 10 questions in a quiz?)
qIndex += 1 ' < -----'it is, 'then set the current question here since we are using an index
LoadQuestion()
UpdateLabel()
lblCorrectAnswers.ResetText()
rb1.Checked = False
rb2.Checked = False
rb3.Checked = False
rb4.Checked = False
End If
End Sub
Private Sub LoadQuestion()
If AllQuestions.Count = 0 Then Exit Sub
updatingQuestion = True
curQandA = AllQuestions.ElementAt(qIndex)
lbQuestion.Text = curQandA.Question
rb1.Text = curQandA.Answers.ElementAt(0).AnswerText
rb2.Text = curQandA.Answers.ElementAt(1).AnswerText
rb3.Text = curQandA.Answers.ElementAt(2).AnswerText
rb4.Text = curQandA.Answers.ElementAt(3).AnswerText
rb1.Checked = curQandA.Answers.ElementAt(0).AnswerChecked
rb2.Checked = curQandA.Answers.ElementAt(1).AnswerChecked
rb3.Checked = curQandA.Answers.ElementAt(2).AnswerChecked
rb4.Checked = curQandA.Answers.ElementAt(3).AnswerChecked
updatingQuestion = False
End Sub
Sub UpdateLabel()
lblNumber.Text = (qIndex + 1).ToString
End Sub
Private Sub Rbs_CheckedChanged(sender As Object, e As EventArgs) Handles rb4.CheckedChanged, rb3.CheckedChanged, rb2.CheckedChanged, rb1.CheckedChanged
If Not updatingQuestion Then 'when we call LoadQuestion we don't want any of these events to trigger
Dim rb = DirectCast(sender, RadioButton)
Dim qandaAnswer = curQandA.Answers.Where(Function(a) a.AnswerText = rb.Text).FirstOrDefault
If Not qandaAnswer Is Nothing Then qandaAnswer.AnswerChecked = True
For Each ans In curQandA.Answers
If ans Is qandaAnswer Then
ans.AnswerChecked = True
Else
ans.AnswerChecked = False
End If
Next
End If
End Sub
Private Sub BtnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
If AllQuestions.Count = 0 Then Exit Sub
curQandA = AllQuestions.ElementAt(qIndex)
If qIndex > 0 Then
qIndex -= 1
LoadQuestion()
UpdateLabel()
rb1.Checked = False
rb2.Checked = False
rb3.Checked = False
rb4.Checked = False
lblPerRight.ResetText()
End If
End Sub
Private Sub loadme(filename As String)
Timer1.Enabled = True
Using sr As New StreamReader(filename)
While Not sr.EndOfStream
Dim data() As String = sr.ReadLine.Split(","c)
Dim qAndA As New QuestionWithAnswers
qAndA.ItemNumber = Integer.Parse(data(0))
qAndA.Question = data(1)
Dim answers() = data(2).Split("|"c)
For Each answer In answers
qAndA.Answers.Add(New QuestionWithAnswers.Answer With {.AnswerText = answer, .AnswerChecked = False})
Next
qAndA.CorrectAnswer = data(3)
AllQuestions.Add(qAndA)
UpdateLabel()
End While
End Using
'get the first question qIndex starts at 0 - first element
curQandA = AllQuestions.ElementAt(qIndex)
LoadQuestion()
End Sub
Private Sub BtnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
If AllQuestions.Count = 0 Then Exit Sub
Dim correctAnswers As Integer = 0
For Each question As QuestionWithAnswers In AllQuestions
For Each answer As QuestionWithAnswers.Answer In question.Answers '<---------How to validate if wrong answer picked?
If answer.AnswerChecked AndAlso answer.AnswerText = question.CorrectAnswer Then
correctAnswers += 1
Exit For
End If
Next
Next
' MessageBox.Show(String.Format("You have correctly answered {0} out of {1} questions!", correctAnswers, AllQuestions.Count.ToString)) '<-----better grading system
' lblCorrectAnswers.Text = String.Format("You have correctly answered {0} out of {1} questions!", correctAnswers, AllQuestions.Count.ToString)
lblPerRight.Text = String.Format("{0}% Correct!", correctAnswers * 10)
End Sub
Private Sub BtnTryAgain_Click(sender As Object, e As EventArgs) Handles btnTryAgain.Click
rb1.Checked = False
rb2.Checked = False
rb3.Checked = False
rb4.Checked = False
For Each question As QuestionWithAnswers In AllQuestions
For Each answer As QuestionWithAnswers.Answer In question.Answers
answer.AnswerChecked = False
Next
Next
qIndex = 0
LoadQuestion()
lblPerRight.ResetText()
lblNumber.Text = "1"
lblCorrectAnswers.ResetText()
End Sub
'Private Sub QuizItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'cast the "sender" "Leviticus (1).mp3"to a ToolStripMenuItem
'Dim tsmi As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
' loadme(DirectCast(sender, ToolStripMenuItem).Tag.ToString)
'convert the Tag (Object) back to a string to get the full file path name from the ToolStripMenuItem that was clicked
'Dim FileToLoad As String = tsmi.Tag.ToString
'just to show you the full file path and name from the ToolStripMenuItem Tag
'AllQuestions.Clear()
' loadme(FileToLoad)
' End Sub
End Class
I put some questions in the comments of the code also.
Thanks for any input, its a project for my church quizzing team!