CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2013
    Posts
    6

    Displaying/Calculating the score in a test.

    Hi, I've got a problem with my spelling program where I can't display the correct score at the end of the test. Well, I can't actually display the score at all... Instead, it just displays the actual answers which is not what I want.
    Btw. I'm using Access to store all the data if you wonder.
    This is the code that I've so far:

    Code:
    Public Class frmTest
        Dim Conn As New System.Data.OleDb.OleDbConnection()
        Dim Score As Integer = 0
        Dim Ansrs(1, 9) As String
        Dim r As Integer = 1
        Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles    btnClose.Click
            ' When a user will press the exit button, a message box will appear asking whether they want to close       the program and if a user clicks 'Yes' then 
            ' the whole program will close but if they click 'No' then the message box will simply dissapear 
            Dim Result As DialogResult
            Result = MessageBox.Show("Are you sure you wish to close the program?", "Close program?", MessageBoxButtons.YesNo)
            If Result = Windows.Forms.DialogResult.Yes Then
                ' Makes sure that the program will actually fully close
                Application.Exit()
            Else
            End If
        End Sub
    
        Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
            ' Instead of just closing the form, it closes the form so that when you open it again, everything    resets
            Me.Dispose()
        End Sub
    
        Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Makes all the things invisible before the actual test starts
            lblDef.Visible = False
            lblWeekNo2.Visible = False
            lblInfo.Visible = False
            lblDefinition.Visible = False
            txtAns.Visible = False
            lblWeekNo.Visible = False
            btnNxt.Visible = False
            lblAnswer.Visible = False
            ' Opening the connection
            Conn = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=Database for VB.accdb")
            Conn.Open()
        End Sub
    
        Private Sub btnNxt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNxt.Click
            If r = 10 Then
                Ansrs(0, r - 1) = txtAns.Text
                txtAns.Text = ""
                FinishTest()
            Else
                Ansrs(1, r - 1) = txtAns.Text
                r = r + 1
                txtAns.Text = ""
                StartTest()
            End If
        End Sub
        Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
            ' Makes all the buttons that you will need in the test, visible. It'll show the field for the user to complete
            txtAns.Visible = True
            lblWeekNo.Visible = True
            lblWeekNo2.Visible = True
            lblDef.Visible = True
            lblDefinition.Visible = True
            btnStart.Visible = False
            lblAnswer.Visible = True
            ' When the button 'Start the test!' is clicked, the 'StartTest' sub routine will start
            StartTest()
        End Sub
        Private Sub StartTest()
            ' Making the 'Next' button visible to allow the user to complete the whole test
            btnNxt.Visible = True
            ' Reading data from the database
            Dim StringStart As String = "SELECT * FROM TestData WHERE WordNumber = " & r
            Dim Comm As New OleDb.OleDbCommand(StringStart, Conn)
            Dim RDR As OleDb.OleDbDataReader = Comm.ExecuteReader
            RDR.Read()
            lblDef.Text = RDR.Item("Definition")
            Ansrs(0, r - 1) = RDR.Item("Answer")
            lblWeekNo2.Text = RDR.Item("WeekNumber")
            lblInfo.Visible = True
            lblInfo.Text = r & "/10"
        End Sub
        Private Sub FinishTest()
            lblInfo.Text = "You have successfully completed the test! Well done!"
            lblInfo.Left = (Me.Width / 1.5) - (lblInfo.Width / 1.5)
            ' When the test is done, it makes all of the things that the user won't need anymore, invisible
            lblDef.Visible = False
            lblWeekNo2.Visible = False
            btnNxt.Visible = False
            lblWeekNo.Visible = False
            txtAns.Visible = False
            lblAnswer.Visible = False
            lblDefinition.Visible = False
            TestScore()
        End Sub
        Private Sub TestScore()
            For r As Integer = 0 To 9
                MsgBox(Ansrs(0, r))
                If Ansrs(0, r).ToLower = Ansrs(1, r).ToLower Then
                    Score = Score + 2
                    MsgBox("You have scored " & Score)
                End If
            Next
        End Sub
    End Class
    Thanks in advance and I hope that somebody will be able to help me!

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Displaying/Calculating the score in a test.

    You need one more subroutine to CalculateScores


    Code:
        Private Sub FinishTest()
            lblInfo.Text = "You have successfully completed the test! Well done!"
            lblInfo.Left = (Me.Width / 1.5) - (lblInfo.Width / 1.5)
            ' When the test is done, it makes all of the things that the user won't need anymore, invisible
            lblDef.Visible = False
            lblWeekNo2.Visible = False
            btnNxt.Visible = False
            lblWeekNo.Visible = False
            txtAns.Visible = False
            lblAnswer.Visible = False
            lblDefinition.Visible = False
            CalculateScores()
            TestScore()
        End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Dec 2013
    Posts
    6

    Re: Displaying/Calculating the score in a test.

    Quote Originally Posted by dglienna View Post
    You need one more subroutine to CalculateScores


    Code:
        Private Sub FinishTest()
            lblInfo.Text = "You have successfully completed the test! Well done!"
            lblInfo.Left = (Me.Width / 1.5) - (lblInfo.Width / 1.5)
            ' When the test is done, it makes all of the things that the user won't need anymore, invisible
            lblDef.Visible = False
            lblWeekNo2.Visible = False
            btnNxt.Visible = False
            lblWeekNo.Visible = False
            txtAns.Visible = False
            lblAnswer.Visible = False
            lblDefinition.Visible = False
            CalculateScores()
            TestScore()
        End Sub
    Isn't that it though?
    Private Sub TestScore()
    For r As Integer = 0 To 9
    MsgBox(Ansrs(0, r))
    If Ansrs(0, r).ToLower = Ansrs(1, r).ToLower Then
    Score = Score + 2
    MsgBox("You have scored " & Score)
    End If
    Next
    End Sub

    And if so, what's wrong with it?

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Displaying/Calculating the score in a test.

    Nope. Converts the string to a number and adds 2 (for some unknown reason)

    Use r, and a TotalForStudent(r) counter, and add Ansrs(0, r) to it
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured