Results 1 to 5 of 5

Thread: Feedback, Suggestions, Input vb.net code

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    14

    Feedback, Suggestions, Input vb.net code

    Hello everyone!
    I have a piece of code I would like to post here for some feedback and suggestions if somebody is willing to help me out. I left comments in the code as to what I need feedback on or suggestions. This is a quizzing app for my church youth quiz team. How can I make it better! Thanks!

    Code:
    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

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Feedback, Suggestions, Input vb.net code

    1) I'd move the Answer class out of the other class. This is more aesthetic than anything else, but a class defined within a different class should probably only be used by the containing class. If it is exposed outside of the container, as Answer is, then it ought to be a first class....class. It's not harmful as you have it, just a non-standard approach.

    2) If you want qIndex to ever go above 9, then you'd need to have the 9 be a variable itself. So, you might have something like qLimit. The user would be able to change that (with a NumericUpDown control, perhaps, or just two buttons for + and -). The code would be essentially the same, as you'd just be saying: If qIndex < qLimit Then, rather than what you have now. qIndex would still increment up to whatever the limit is. Of course, any other place that you hardcoded in the current limit would have to also be changed to respond to qLimit rather than 9.

    3) You don't really need ElementAt. A list works like an array (which it wraps, anyways), so you can just say AllQuestions(qIndex).

    4) In the checkedChanged, you can simplify the If statement down to a single line, if you choose:

    Code:
     If ans Is qandaAnswer Then
                        ans.AnswerChecked = True
                    Else
                        ans.AnswerChecked = False
                    End If
    becomes
    Code:
    ans.AnswerChecked = ans Is qandaAnswer
    5) I don't understand the question in red. It seems like you already have a mechanism for determining the right answer, so the number of wrong answers is known (total number minus number right), so what are you asking for?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    14

    Re: Feedback, Suggestions, Input vb.net code

    Thank you for these suggestions, I see where they make sense. I will apply those updates the code. (Never thought of the qLimit, nice ) As to the last part, Im looking for
    a way to show a dialog box that says something to the effect "Sorry, Wrong Answer" and then move on to next question. Of course this line
    Code:
    lblPerRight.Text = String.Format("{0}% Correct!", correctAnswers * 10)
    would have to reflect that they have given a wrong answer.

    One last thing I would like to ask feed back on: how would I go about projecting the questions onto the churches pulpit monitor where everyone can see the question. Not the whole application, that would only be seen on the computer at the media center. As I fire up the application I can run it from the media center and show the questions on the churches monitor. Hope that makes sense. Thanks for this invaluable feedback!

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Feedback, Suggestions, Input vb.net code

    Quote Originally Posted by Luigi Nesbitt View Post
    One last thing I would like to ask feed back on: how would I go about projecting the questions onto the churches pulpit monitor where everyone can see the question. Not the whole application, that would only be seen on the computer at the media center. As I fire up the application I can run it from the media center and show the questions on the churches monitor.
    In order for the two screens to show different things, they need to be set up in the display properties as separate monitors (if they aren't, it should be easy enough to change).

    In terms of your application, all you need is a second form which just shows the questions (preferably using a large font), which you can simply drag onto the correct screen then maximize.

    Assuming the new form is called FormDisplayQuestions, and it contains a Label called Label1, you could use it like this:
    Code:
        Private FormDisplay As New FormDisplayQuestions
    Code:
            FormDisplay.Label1.Text = "Question 1"
            FormDisplay.Show

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    14

    Re: Feedback, Suggestions, Input vb.net code

    Quote Originally Posted by si_the_geek View Post
    In order for the two screens to show different things, they need to be set up in the display properties as separate monitors (if they aren't, it should be easy enough to change).

    In terms of your application, all you need is a second form which just shows the questions (preferably using a large font), which you can simply drag onto the correct screen then maximize.

    Assuming the new form is called FormDisplayQuestions, and it contains a Label called Label1, you could use it like this:
    Code:
        Private FormDisplay As New FormDisplayQuestions
    Code:
            FormDisplay.Label1.Text = "Question 1"
            FormDisplay.Show
    Great, thanks for this suggestion, Ill give it a go and post back if succeeded!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width