I have a long list of jokes and I wish to select them randomly from a
big file. What is teh best way to select them? Ordinary Text or XML?
Also how do I index them? I know howto read text files so I suppose I
could use a separator of some sort bewteen jokes but I am thinking
that they may all need to be read into an array first?
Thanks
K.
If you can list your jokes line by line in a text file:
You can read your text content using StreamReader,
and assign the content to a multi-line textbox:
'------Code Begins-------
Imports System.IO
Using reader As New StreamReader("c:\jokes.txt")
' I recommend setting "Wordwrap" to False for
' textbox not to force long lines to be misaligned.
TextBox1.Text = reader.ReadToEnd
End Using
'----Code Ends--------
After loading all the jokes into a textbox(even you can hide the
textbox by making its visible property to False), and now you can
select them randomly as you can use textbox's "lines" property with
Random class:
'------Code Begins-------
' Like that:
Dim rand As New Random
Dim selectedRand As String
selectedRand = TextBox1.Lines _
(rand.Next(0, TextBox1.Lines.Length))
' eg: Display random joke in MsgBox
MsgBox(selectedRand)
'-----Code Ends------
Plus, you can make the textbox read-only not to be modified by user,
and make it invisible not to be seen by user depending on your wish.
Hope this helps,
Onur Güzel