• Congratulations to the Class of 2024 on your results!
    Let us know how you went here
    Got a question about your uni preferences? Ask us here

VB problem (1 Viewer)

Garick

Member
Joined
Jun 22, 2003
Messages
48
Location
Penrith
this aint a huge problem at the moment, but i still need help

i need to read a random line from a text file and putread line into a textbox.

ive already set up the EOF loop to find the file length, but i cant figure otu the following

1. How would I pick a random line from the file? use rnd * filelength?

2. How would i put said line in a textbox?


all help appreciated.
 

del

Member
Joined
Jul 8, 2002
Messages
412
Gender
Undisclosed
HSC
N/A
Treat the file as a random file

To open it as a random file:

Code:
Open "FileNameHere" For Random As 1 Len = XxXxX

Where XxXxX is the length of the longest line of text

Then to read

Code:
    Dim lineRead As String
    Dim recNo As Integer
    Randomize
    recNo = Int(Rnd * 8) + 1
    Get #1, recNo, lineRead
    txtWhatever.text = lineRead

In this situation 8 is the number of lines in the text file, so it should be changed to however lines you have
 

Fosweb

I could be your Doctor...
Joined
Jun 20, 2003
Messages
594
Location
UNSW. Still.
Gender
Male
HSC
2003
hmm... that means you first have to know the longest line of text. will have mega speed problems if the file is big.

why not just:
-------------------------------------
1. Open the file
2. Count the number of lines
3. Close the file
-------------------------------------
4. Choose a random number between 1 and the number of lines
5. Open file again
6. Read each line until you get to the random number
7. Close file.
-------------------------------------

For 1st bit:
Code:
Open file for Input as #1
Do Until EOF(1)
Line Input #1, nextLine
lineCount = lineCount + 1
Loop
Close #1
For next bit:
Code:
'Random number shtuff
Randomize
rndNum = int(rnd*linecount) + 1)

Open file For Input as #1
Do Until EOF(1)
line input #1, nextReadLine
readLinesCount = readLinesCount + 1
If readLinesCount = rndNum Then textbox.text = nextReadLine
Loop
Close #1
Sorry - untested - but it should work no probs... and then you dont have to worry about random access screwing up because you dont actually know how much text is in each line...
 

Fosweb

I could be your Doctor...
Joined
Jun 20, 2003
Messages
594
Location
UNSW. Still.
Gender
Male
HSC
2003
Ok - so I ended up making it... and it does work.

Also wanted to add a comment:
Unless you know exactly how the file you are reading is formatted, (i.e. spaces between variables/lengths of sections of file) you cant use random access and get results that are nice...

Check the attachment.
 
Last edited:

del

Member
Joined
Jul 8, 2002
Messages
412
Gender
Undisclosed
HSC
N/A
random files work when you know the structure of your file..

depends on the type of application really.. e.g. hangman game with a text file containing the words, or where you need to work with an exported database file of some sort..

of course if you don't know the length of the longest sentence because your files contents change, foster's code is the way to go for the fact your gonna have to loop anyway
 

Fosweb

I could be your Doctor...
Joined
Jun 20, 2003
Messages
594
Location
UNSW. Still.
Gender
Male
HSC
2003
structure - thats the word i was after:

hehe... delete where i said 'file you are reading is formatted' and put structured in there.
 

Fosweb

I could be your Doctor...
Joined
Jun 20, 2003
Messages
594
Location
UNSW. Still.
Gender
Male
HSC
2003
oh - by the way... when you say you have already found file length, do you mean characters in the file?
because you can just return the LOF(filenumber) to get the length...
 

Garick

Member
Joined
Jun 22, 2003
Messages
48
Location
Penrith
whenm i said file length, i meant number of lines in the file.

the thing is, i dont know the file length, because the file could be altered by a user (which i am allowing) and then the file length would change.

so im using a funciton to chekc the file length first, then get a rnadom line.


thanks for all the help guys.,
 

del

Member
Joined
Jul 8, 2002
Messages
412
Gender
Undisclosed
HSC
N/A
you'll have to use fosters way then :)
 

Jinglebell

Member
Joined
Mar 2, 2003
Messages
381
Location
over here!!!
Gender
Female
HSC
2003
my program kinda does this, i don't know if this is the best way, but it was the easiest way i could find. basically, thelines in the text file are put into an array, and then a random element is chosen from the array. i only have one word on each line, i don't know if multiple words would make a difference. here's my code:

to read answers and assign to array...
Code:
    Open App.Path & "\" & Level & "Answers.txt" For Input As #1
    x = 0
    While Not EOF(1)
        x = x + 1
        Input #1, answer(x)
    Wend
    Close #1
then to randomly chose an answer...
Code:
    rnum1 = Int(Rnd * 10) + 1
    currentAns = answer(rnum1)
hope that clears it up a little for ya...
 

del

Member
Joined
Jul 8, 2002
Messages
412
Gender
Undisclosed
HSC
N/A
a problem with that is if the file you are reading is very large, storing the data in an array may consume alot of memory which has its consequences....
 

Fosweb

I could be your Doctor...
Joined
Jun 20, 2003
Messages
594
Location
UNSW. Still.
Gender
Male
HSC
2003
but - it is also A LOT faster if you have, say, less that 50000 lines of text...

[ (A LOT) ^ (A LOT) ]

Sorry: Edit: A lot faster if you have a significant number of lines.
Not noticably faster if you have only a few lines...
 
Last edited:

del

Member
Joined
Jul 8, 2002
Messages
412
Gender
Undisclosed
HSC
N/A
true, but depends on i guess the programs minimum system requirements........ i had a similar problem last yr cos my program had to work for high schools of up to 1000+ students plus teachers and storing arrays of records was slowing down the program heaps

if you know there is no possibility of your program being run on a very ancient computer it's not a problem... as speed and memory issues wont be a prob.....
 

Garick

Member
Joined
Jun 22, 2003
Messages
48
Location
Penrith
well, it all worked out, its quite interesting now. i have been buildign a dnd char generator, and i used txt file to store arrays of random names and such.

i also discovered a VB form can only have 255 objects on it. isnt that weird? well, no, but im hoping to christ i never have that many obects again.

EDIT: now for problem 2, i could probably figure this out on my own, but im so busy placing objects and coding i havent had time to. how would i go about setting up a button on a form, which would print the form off, minus the print button?
 
Last edited:

Fosweb

I could be your Doctor...
Joined
Jun 20, 2003
Messages
594
Location
UNSW. Still.
Gender
Male
HSC
2003
Problem One: Yep - you can only have 255 controls on a form, however control arrays only count to 1 of these... But they have this limit for good reason - performance becomes a big issue...

Problem Two:

what kind of contents (like - what types of controls do you need to print off) on your form? If its not just a background picture then your contents wont print properly with .PrintForm

have you done much/any api programming?
the best way i can think of, if its not just a form or a picture box that you want to print (i.e. you want its containing controls) is to: (you will have to use api):

Copy all the controls as bitmaps to a picture box (which you could have hidden behind them for example, or on another form...)
Then print out that picture box.

Have a look at this link: http://www.andreavb.com/tip070013.html
It will tell you all...

HTH..
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top