Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm trying to make an app that is a multiple choice quiz. I have one question. I have a button click event that loads 4 lines of text to 4 separate textboxes, how can I click the same button again to add the next 4 lines of text in a text file to the textboxes? This is my code so far:


VB
Public Class Form1
    Dim lines() As String
    Dim index As Integer = 0
    Dim answers() As String


VB
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        lines = IO.File.ReadAllLines("C:\Quiz\GenesisQuiz.txt")
        answers = IO.File.ReadAllLines("C:\Quiz\GenesisAnswers.txt")



VB
Private Sub Button40_Click(sender As Object, e As EventArgs) Handles Button40.Click
        TextBox1.Text &= lines(index) & Environment.NewLine
        index += 1
        If index > lines.GetUpperBound(0) Then Button40.Enabled = False
    End Sub


The above code loads my questions to a textbox 1 at a time as I hit the next button.


VB
Private Sub Button40_MouseClick(sender As Object, e As MouseEventArgs) Handles Button40.MouseClick
        If File.Exists("C:\Quiz\GenesisAnswers.txt") Then
            Dim ioFile As New StreamReader("C:\Quiz\GenesisAnswers.txt")
            TextBox2.Text = ioFile.ReadLine()
            TextBox3.Text = ioFile.ReadLine()
            TextBox4.Text = ioFile.ReadLine()
            TextBox5.Text = ioFile.ReadLine()
        End If
    End Sub


This code loads my 4 answers to 4 separate textboxes with radio buttons at the right of the textbox to choose which answer to give. I need help on loading the next 4 answers in my text file to the 4 separate textboxes as the new question, question 2, is loaded. Thanks for your constructive criticism!
Posted
Updated 12-Feb-15 5:34am
v2

If this a windows form application,
declare the file variable in form scope, instead of local. Open the file in form load.

If this is a web application, you need to keep the last line-no in a hidden field. On next click skip upto that mark before loading the next lines in your textboxes.
 
Share this answer
 
Hi,

Please Use below code.

VB
Private Function ReadAnswers(ByVal Offset As Integer) As ArrayList

       Dim arrAnswers As New ArrayList

       'Read all lines from the text file and fill the lines array
       Dim strLines() As String = File.ReadAllLines(Application.StartupPath & "\1.txt")


       Dim StartIndex As Integer = ((Offset - 1) * 4)
       Dim EndIndex As Integer = StartIndex + 3

       For i = StartIndex To EndIndex
           arrAnswers.Add(strLines(i).ToString)
       Next
       Return arrAnswers
   End Function


In above function you can passed the Question No. as a parameter of offset.
Please let me know if you have any query.

Thanks.!!!
Hiren Lad.
 
Share this answer
 
Comments
Luigi Nesbitt 12-Feb-15 11:31am    
Im sorry Hiren Lad but this code is not populating my 4 text boxes with the next 4 answers in my GenesisAnswers.txt file. As it is now, I just click next button and it loads the 1st Question in TextBox1 and 4 different answers to TextBox2, TextBox3...etc. Now I wanna click my next button for Question 2 - which gets loaded into TextBox1 but the next 4 answers in my GenesisAnswers.txt file do not get loaded - Just the same 4 answers for question 1 gets loaded. Maybe I have not rightly implemented the code you gave me? I added the Function code below the next button40. Could you clarify for me please. Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900