Score

Hi,

I've recently started using Visual Basic 2005 express edition, so I'm pretty new to the language and so. Though I'm getting the hang of it! But I have one question:

I'm trying to make some questions in my program, something like this:

Translate the following sentences from English to French:

Do you live in England?

................................................................[textbox]

PrivateSub Button1_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button1.Click

If TextBox1.Text ="Tu es all en Angleterre?"Then

MessageBox.Show("Good!")

Else : MessageBox.Show("False!")

EndIf

EndSub

Then if you type it in correctly, it sais in a messagebox Good or False.

That works great, but I want more, haha!

I've made a form, with a few questions like this. All works well with the Good or False thing. I've also placed a button which says: Score.

Now I want to keep track of the score, like 5/10 questions were good, and 10/10 were false. So when you push the 'Score' button there is a messagebox which says the score... I really have no idea how to do this!?

I hope you can help me with this,

Thanks, Francois

[2225 byte] By [Francoisvdv] at [2007-12-24]
# 1
Anyone? :(
Francoisvdv at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

You need to create a couple of class level variables for the form - One to handle the number of questions and the second to count the correct answers.

These variables will be accessible to any of the methods within the form. So when you get a correct answer you simply need to increment the variable on the correct answers and for every question you will increment the questions counter.

The general psuedocode for this is

Public Class Form1
Private iquestions As Integer = 0
Private icorrect As Integer = 0

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'//Answer Validation Logic

iquestions = iquestions + 1
'if answer if correct then
icorrect = icorrect + 1
'end if
End Sub
End Class

All you need to do is code up your correct answer logic to increment the variables when you validate the question to determine if the correct answer was given.

At any time within the form you can display the amount of questions answered and the number of correctly answered.

spotty at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

Thought you might find this interesting:

#1 Create a New Project
#2 Paste the Form1.Designer code over the Form1.Designer.VB file (click Show All Files in the solution explorer and expand Form1, double click Form1.Designer.VB)
#3 Paste the Form1.VB code over the Form1 code file page
#4 Add a new XML document and paste QA.XML into it

Code follows:


Form1.Designer.VB

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _

Partial Class Form1

Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.

<System.Diagnostics.DebuggerNonUserCode()> _

Protected Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing AndAlso components IsNot Nothing Then

components.Dispose()

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

<System.Diagnostics.DebuggerStepThrough()> _

Private Sub InitializeComponent()

Me.InstructionLabel = New System.Windows.Forms.Label

Me.QuestionLabel = New System.Windows.Forms.Label

Me.AnswerTextBox = New System.Windows.Forms.TextBox

Me.SubmitButton = New System.Windows.Forms.Button

Me.ScoreLabel = New System.Windows.Forms.Label

Me.SuspendLayout()

'

'InstructionLabel

'

Me.InstructionLabel.AutoSize = True

Me.InstructionLabel.Location = New System.Drawing.Point(13, 13)

Me.InstructionLabel.Name = "InstructionLabel"

Me.InstructionLabel.Size = New System.Drawing.Size(224, 13)

Me.InstructionLabel.TabIndex = 0

Me.InstructionLabel.Text = "Translate the following sentences into French:"

'

'QuestionLabel

'

Me.QuestionLabel.AutoSize = True

Me.QuestionLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D

Me.QuestionLabel.Location = New System.Drawing.Point(16, 43)

Me.QuestionLabel.Name = "QuestionLabel"

Me.QuestionLabel.Size = New System.Drawing.Size(2, 15)

Me.QuestionLabel.TabIndex = 1

'

'AnswerTextBox

'

Me.AnswerTextBox.Location = New System.Drawing.Point(16, 72)

Me.AnswerTextBox.Name = "AnswerTextBox"

Me.AnswerTextBox.Size = New System.Drawing.Size(468, 20)

Me.AnswerTextBox.TabIndex = 2

'

'SubmitButton

'

Me.SubmitButton.Location = New System.Drawing.Point(409, 98)

Me.SubmitButton.Name = "SubmitButton"

Me.SubmitButton.Size = New System.Drawing.Size(75, 23)

Me.SubmitButton.TabIndex = 3

Me.SubmitButton.Text = "Submit"

Me.SubmitButton.UseVisualStyleBackColor = True

'

'ScoreLabel

'

Me.ScoreLabel.AutoSize = True

Me.ScoreLabel.Location = New System.Drawing.Point(13, 103)

Me.ScoreLabel.Name = "ScoreLabel"

Me.ScoreLabel.Size = New System.Drawing.Size(169, 13)

Me.ScoreLabel.TabIndex = 4

Me.ScoreLabel.Text = "0 Answered, 0 Correct, 0 Incorrect"

'

'Form1

'

Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)

Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

Me.ClientSize = New System.Drawing.Size(496, 139)

Me.Controls.Add(Me.ScoreLabel)

Me.Controls.Add(Me.SubmitButton)

Me.Controls.Add(Me.AnswerTextBox)

Me.Controls.Add(Me.QuestionLabel)

Me.Controls.Add(Me.InstructionLabel)

Me.Name = "Form1"

Me.Text = "Q&A"

Me.ResumeLayout(False)

Me.PerformLayout()

End Sub

Friend WithEvents InstructionLabel As System.Windows.Forms.Label

Friend WithEvents QuestionLabel As System.Windows.Forms.Label

Friend WithEvents AnswerTextBox As System.Windows.Forms.TextBox

Friend WithEvents SubmitButton As System.Windows.Forms.Button

Friend WithEvents ScoreLabel As System.Windows.Forms.Label

End Class

Form1.VB

Public Class Form1

Private xmlrdr As New Xml.XmlTextReader("qa.xml")

Private totalquestions As Integer = 0

Private correctanswers As Integer = 0

Private wronganswers As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

xmlrdr.ReadToFollowing("qa")

Me.totalquestions += 1

Me.QuestionLabel.Text = xmlrdr.GetAttribute("question")

End Sub

Private Sub GetNextQuestion()

If Not Me.xmlrdr.ReadToNextSibling("qa") Then

MessageBox.Show(String.Format("You got a {0}%.", (Math.Round(Me.correctanswers / Me.totalquestions, 2)) * 100), "You have answered all the questions!", MessageBoxButtons.OK, MessageBoxIcon.Information)

Else

Me.totalquestions += 1

Me.QuestionLabel.Text = xmlrdr.GetAttribute("question")

End If

End Sub

Private Sub SubmitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubmitButton.Click

If Me.AnswerTextBox.Text = xmlrdr.GetAttribute("answer") Then

MessageBox.Show("Correct!", "Question Answered", MessageBoxButtons.OK, MessageBoxIcon.Information)

Me.correctanswers += 1

Else

MessageBox.Show(String.Format("Sorry, the correct answer is:{0}{1}", ControlChars.NewLine, xmlrdr.GetAttribute("answer")), "Question Answered", MessageBoxButtons.OK, MessageBoxIcon.Error)

Me.wronganswers += 1

End If

Me.AnswerTextBox.Text = String.Empty

Me.ScoreLabel.Text = String.Format("{0} Answered, {1} Correct, {2} Incorrect", Me.totalquestions, Me.correctanswers, Me.wronganswers)

Me.GetNextQuestion()

End Sub

End Class

QA.XML

<?xml version="1.0" encoding="utf-8" ?>

<questions>

<qa question="Do you live in England?" answer="Tu es all en Angleterre?" />

<qa question="Do you speak French?" answer="Parlez-vous fran?ais?" />

<qa question="The cat ran up the tree." answer="Le chat a fonctionn vers le haut de l'arbre." />

<qa question="There are four wheels on the yellow bus." answer="Il y a quatre roues sur l'autobus jaune." />

<qa question="Many people like red hats." answer="Beaucoup de gens aiment les chapeaux rouges." />

<qa question="I like to run in the sun." answer="J'aime courir au soleil." />

<qa question="How high can you jump?" answer="Combien haut pouvez-vous sauter?" />

<qa question="What is your favorite color?" answer="Quelle est votre couleur prfre?" />

<qa question="There are seven people in my class." answer="Il y a sept personnes dans ma classe." />

<qa question="Tomorrow is my father’s birthday." answer="Le demain est l'anniversaire de mon père." />

<qa question="Vanillia is my favorite flavor of icecream." answer="Vanillia est ma saveur prfre de glace." />

</questions>

Hope that helps!

rkimble at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

Hi,

This is exactly what I'm looking for! Thank you very much!

Francoisvdv at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5

Hello,

Looks pretty complicated! Though I'll give it a try tomorrow. Thanks

Francoisvdv at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...