subo76
asked on
Reading randomly from text files and displaying it.
I have a text file in which there are 5 lines. abc.txt
Question 1
Question 2
Question 3
Question 4
Question 5
I would like my VB program to read randomly from this text file and display the question on the VB form one by one. On the click of button or click of left key of mouse next question is shown.Please tell me how to go about it.
Question 1
Question 2
Question 3
Question 4
Question 5
I would like my VB program to read randomly from this text file and display the question on the VB form one by one. On the click of button or click of left key of mouse next question is shown.Please tell me how to go about it.
the form load event should have been
Private Sub Form_Load()
Randomize
ReDim sQuestions(1, 0)
Open "c:\questions.txt" For Input As #1
Do Until EOF(1)
c = c + 1
ReDim Preserve sQuestions(1, c)
Line Input #1, sQuestions(0, c)
Loop
Close #1
End Sub
Private Sub Form_Load()
Randomize
ReDim sQuestions(1, 0)
Open "c:\questions.txt" For Input As #1
Do Until EOF(1)
c = c + 1
ReDim Preserve sQuestions(1, c)
Line Input #1, sQuestions(0, c)
Loop
Close #1
End Sub
deighton,
I had a look at you code ad tested it.
Just one thing I would like to add is if you have a blank line in the code it will show up as a question. Throw in an if statement and it should be fine.
P.s. Nice Code.
Ed.
I had a look at you code ad tested it.
Just one thing I would like to add is if you have a blank line in the code it will show up as a question. Throw in an if statement and it should be fine.
P.s. Nice Code.
Ed.
sorry that should be:
if you have a blank line in the text file. It will show up as a question in the text box.
if you have a blank line in the text file. It will show up as a question in the text box.
ASKER
Sorry I forgot to mention that the same line in which I have Question 1 in the text file has answer seperated by tab.
something like this.
Question1 Answer:abc
"there is a tab spacing between the question 1 and answer"
I need to read the question and display it to the user randomly. the code which u sent works fine. Now in the same form I need a checkbox , which if checked should display the answer in the next text box otherwise not.
Thanks in advance
something like this.
Question1 Answer:abc
"there is a tab spacing between the question 1 and answer"
I need to read the question and display it to the user randomly. the code which u sent works fine. Now in the same form I need a checkbox , which if checked should display the answer in the next text box otherwise not.
Thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Dim sQuestions() As String
Dim c As Long
Private Sub Command1_Click()
Dim lR As Long
Dim n As Long
Dim k As Long
lR = Int(c * Rnd + 1)
If c = 0 Then MsgBox "no more questions": Exit Sub
n = 0
k = 0
While n < lR
k = k + 1
If sQuestions(1, k) = "" Then
n = n + 1
End If
Wend
Text1.Text = sQuestions(0, k)
sQuestions(1, k) = "N"
c = c - 1
End Sub
Private Sub Form_Load()
Randomize CLng(Now)
ReDim sQuestions(1, 0)
Open "c:\questions.txt" For Input As #1
Do Until EOF(1)
c = c + 1
ReDim Preserve sQuestions(1, c)
Line Input #1, sQuestions(0, c)
Loop
Close #1
End Sub