Inputting From Text File (Newbie)
Inputting From Text File (Newbie)
Inputting From Text File (Newbie)
Inputting From Text File (Newbie)
Inputting From Text File (Newbie)
Inputting From Text File (Newbie) Inputting From Text File (Newbie) Inputting From Text File (Newbie) Inputting From Text File (Newbie) Inputting From Text File (Newbie) Inputting From Text File (Newbie) Inputting From Text File (Newbie) Inputting From Text File (Newbie)
Inputting From Text File (Newbie) Inputting From Text File (Newbie)
Inputting From Text File (Newbie)
Go Back  Xtreme Visual Basic Talk > > > Inputting From Text File (Newbie)


Reply
 
Thread Tools Display Modes
  #1  
Old 01-06-2010, 09:37 AM
byrnen5 byrnen5 is offline
Newcomer
 
Join Date: Jan 2010
Posts: 2
Default Inputting From Text File (Newbie)


Hi Everyone, Happy New Year!

Looking for a bit of help with some code im working on. I want to randomly input a text line from a .txt to different labels in my program. So far i have this:



Dim FILE_NAME As String = "question.txt"

If System.IO.File.Exists(FILE_NAME) = True Then

Dim objReader As New System.IO.StreamReader(FILE_NAME)

Dim question(5) As String
Dim line As String = ""
Dim i As Integer = 0
Do
line = objReader.ReadLine()
i = i + 1
Loop Until i = counter

question = line.Split(vbTab)
lblquest.Text = question(0)
a.Text = question(1)
b.Text = question(2)
c.Text = question(3)
d.Text = question(4)
Text1.Text = question(5)


This works perfectly, but as you can see there is no randomization, it just runs through each line. I want one out of every three lines to be chosen. My text file, on each line, has question tab answer tab answer tab answer tab answer tab correct answer. So for the first inputted question i want it to chose one of the first three lines, second question chose a line between line 4,5 and 6. etc.

Any help would be greatly appreciated.

Regards,

Neil
Reply With Quote
  #2  
Old 01-06-2010, 02:31 PM
Iceplug's Avatar
IceplugInputting From Text File (Newbie) Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

This should not be made into a file access problem. You should read everything that you need from the file and close it when you are done.

What I'd do here is have an array of lines that hold all of the relevant information.

Dim Lines(5) As String

Populate the array using a integer counter that increments when you fill e.g. row i and goes to the next row by saying i = i + 1 so that row i now refers to the next row.
Actually, you already have the counter i setup so now you just use the array.

Say, for example, your lines array contains:
Lines(0) = "What is chemical formula for salt?--H2O--CO2--NH3--NaCl"
Lines(1) = "How many feet in a yard?--2--3--4--5"
Lines(2) = "What play is worth 3 points?--Safety--Touchdown--Fieldgoal--2-point conversion"

You then pick a random number by using the Random class
Dim RNG As Random 'In form declarations!
RNG = New Random 'In form load! Do not place in a subroutine that runs more than once!
Generate a random number by doing:
R = RNG.Next(3) 'Generates a random number less than 3, thusly it generates 0, 1, or 2.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Inputting From Text File (Newbie)
Inputting From Text File (Newbie)
Inputting From Text File (Newbie) Inputting From Text File (Newbie)
Inputting From Text File (Newbie)
Inputting From Text File (Newbie)
Inputting From Text File (Newbie) Inputting From Text File (Newbie) Inputting From Text File (Newbie) Inputting From Text File (Newbie) Inputting From Text File (Newbie) Inputting From Text File (Newbie) Inputting From Text File (Newbie)
Inputting From Text File (Newbie)
Inputting From Text File (Newbie)
 
Inputting From Text File (Newbie)
Inputting From Text File (Newbie)
 
-->