VB+Random+Fact+Program

' This is a random fact generator. The program looks on the c: drive for a text file full of ' random facts. It reads all lines of the file into a list of strings, and then when someone ' presses the button, a random number is chosen and the list entry at that spot is shown in ' a label.

Public Class frmRandomFact

Dim lstFacts As New List(Of String)
 * 1) Region "Declarations (Global Variables and Objects)"
 * 1) End Region


 * 1) Region "When Program Starts"

Sub frmRandomFact_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Create a streamreader object to read from a file Dim sRead As IO.StreamReader = New IO.StreamReader("c:/text_files/random_facts.txt") ' Create a string variable that will hold the line from the file Dim strTempString As String ' Read the first line of the file into the list of strings (better not be a space) lstFacts.Add(sRead.ReadLine) ' Enter the loop that goes until the end of the file Do While (sRead.Peek <> -1) ' Read the next line into the temp string variable strTempString = sRead.ReadLine ' If it wasn't an empty line (newline) ... If strTempString <> "" Then ' ... then add it to the list lstFacts.Add(strTempString) End If Loop End Sub


 * 1) End Region

Private Sub cmdRandomFact_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRandomFact.Click ' Create a random object to use Dim rndRand As New Random ' When someone clicks the button, we pick a number between 0 and the number of items ' in the list, then we show that entry in the label. ' ' For example, if we choose 49 as our random number, the 49th entry in the list will ' appear on the label. lblFact.Text = lstFacts(rndRand.Next(0, lstFacts.Count))
 * 1) Region "When Someone Clicks"

End Sub
 * 1) End Region

Private Sub lblFact_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblFact.Click

End Sub End Class