siradammeathead
January 29th, 2009, 07:54 AM
I am looking to declare an array of 20 intergers to store the answers to the questions and then for the next button i am looking to get the answer and write it to the array. If one radio button is checked then i want it to write 1 to the array. Then finally i want to write the username, the array of answers and score to the txt file. How can I do that?
below is my code:
Imports System
Imports System.IO
Public Class Question
Dim answers(19) As Integer
Sub Main()
For i As Integer = 0 To 19
Do
answers(i) = Console.ReadLine()
Loop Until answers(i) > 0
Next
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
If Not ((txtAnswer.Text = "1" And RadioButton1.Checked) Or (txtAnswer.Text = "2" And RadioButton2.Checked) _
Or (txtAnswer.Text = "3" And RadioButton3.Checked)) Then
'take one live away
lives -= 1
score = (correct * 2) + lives
lblScore.Text = score
txtLives.Text = lives
Else
'if correct +1 to score
correct += 1
score = (correct * 2) + lives
lblScore.Text = score
End If
If lives = 0 Or quizcurrencymanager.Position >= quizcurrencymanager.Count - 1 Then
Dim fw As StreamWriter
Dim readstring As String
Dim readstring2 As String
fw = New StreamWriter("J:\quizfile.txt", True)
fw.Write(ControlChars.CrLf)
readstring = Intro.txtName.Text
readstring2 = lblScore.Text
fw.Write(readstring)
fw.Write(" Score: ")
fw.Write(readstring2)
fw.Close()
If MessageBox.Show(Intro.txtName.Text & " you scored " & score & " points", "Do you wish to try again?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then
End
Else
'restart quiz
Close()
Intro.Show()
Intro.txtName.Clear()
Intro.txtName.Focus()
quizcurrencymanager.Position = 0
lblScore.Text = ""
End If
Else
quizcurrencymanager.Position += 1
End If
End Sub
Private WithEvents quizcurrencymanager As CurrencyManager
Private lives As Integer, correct As Integer
Public score As Integer
Private Sub Question1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'QuestionsDataSet.Questions' table. You can move, or remove it, as needed.
Me.QuestionsTableAdapter.Fill(Me.Questio...
quizcurrencymanager = DirectCast(Me.BindingContext(QuestionsBi... CurrencyManager)
lives = 3
score = 0
correct = 0
txtLives.Text = lives
lblScore.Text = score
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
If MessageBox.Show("Are you sure?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
End
End If
End Sub
End Class
below is my code:
Imports System
Imports System.IO
Public Class Question
Dim answers(19) As Integer
Sub Main()
For i As Integer = 0 To 19
Do
answers(i) = Console.ReadLine()
Loop Until answers(i) > 0
Next
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
If Not ((txtAnswer.Text = "1" And RadioButton1.Checked) Or (txtAnswer.Text = "2" And RadioButton2.Checked) _
Or (txtAnswer.Text = "3" And RadioButton3.Checked)) Then
'take one live away
lives -= 1
score = (correct * 2) + lives
lblScore.Text = score
txtLives.Text = lives
Else
'if correct +1 to score
correct += 1
score = (correct * 2) + lives
lblScore.Text = score
End If
If lives = 0 Or quizcurrencymanager.Position >= quizcurrencymanager.Count - 1 Then
Dim fw As StreamWriter
Dim readstring As String
Dim readstring2 As String
fw = New StreamWriter("J:\quizfile.txt", True)
fw.Write(ControlChars.CrLf)
readstring = Intro.txtName.Text
readstring2 = lblScore.Text
fw.Write(readstring)
fw.Write(" Score: ")
fw.Write(readstring2)
fw.Close()
If MessageBox.Show(Intro.txtName.Text & " you scored " & score & " points", "Do you wish to try again?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then
End
Else
'restart quiz
Close()
Intro.Show()
Intro.txtName.Clear()
Intro.txtName.Focus()
quizcurrencymanager.Position = 0
lblScore.Text = ""
End If
Else
quizcurrencymanager.Position += 1
End If
End Sub
Private WithEvents quizcurrencymanager As CurrencyManager
Private lives As Integer, correct As Integer
Public score As Integer
Private Sub Question1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'QuestionsDataSet.Questions' table. You can move, or remove it, as needed.
Me.QuestionsTableAdapter.Fill(Me.Questio...
quizcurrencymanager = DirectCast(Me.BindingContext(QuestionsBi... CurrencyManager)
lives = 3
score = 0
correct = 0
txtLives.Text = lives
lblScore.Text = score
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
If MessageBox.Show("Are you sure?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
End
End If
End Sub
End Class