How to Create a Random Line Selector in Visual Basic

Language

Introduction: Welcome to my tutorial on how to create a random line selector from a word document. Steps of Creation: Step 1: First we want to create a form with... Textbox1 - File Path Textbox2 - Chosen line Button1 - Browser for file path Button2 - Get random line Step 2: Now lets do the button1 function - selecting a file path...
  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2. Dim fo As New OpenFileDialog
  3. fo.Filter = "Text Files|*.txt"
  4. fo.FilterIndex = 1
  5. fo.ShowDialog()
  6. If (Not fo.FileName = Nothing) Then
  7. TextBox1.Text = fo.FileName
  8. End If
  9. End Sub
We simply create a new file browser and make it so it will only allow the user to select a text file. We then check if it is a correct file (otherwise they have closed the browser) and if so it will make textbox1.text the file path. Step 3: Next we want to make the random line code. First we want to check the file path still exists and then read all the lines in the file and add them all to our list lines variable. We then get a random number and set the text of textbox to the random line.
  1. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  2. If (Not TextBox1.Text = Nothing) Then
  3. If (My.Computer.FileSystem.FileExists(TextBox1.Text)) Then
  4. Dim lines As New List(Of String)
  5. Using sr As New StreamReader(TextBox1.Text)
  6. While sr.Peek <> -1
  7. lines.Add(sr.ReadLine())
  8. End While
  9. End Using
  10. Dim rand As Random = New Random()
  11. Dim r As Integer = rand.Next(lines.Count())
  12. TextBox2.Text = lines(r)
  13. rand = Nothing
  14. r = Nothing
  15. lines = Nothing
  16. End If
  17. End If
  18. End Sub
Project Complete! That's it! Below is the full source code and download to the project files.
  1. Imports System.IO
  2. Public Class Form1
  3. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  4. Dim fo As New OpenFileDialog
  5. fo.Filter = "Text Files|*.txt"
  6. fo.FilterIndex = 1
  7. fo.ShowDialog()
  8. If (Not fo.FileName = Nothing) Then
  9. TextBox1.Text = fo.FileName
  10. End If
  11. End Sub
  12.  
  13. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  14. If (Not TextBox1.Text = Nothing) Then
  15. If (My.Computer.FileSystem.FileExists(TextBox1.Text)) Then
  16. Dim lines As New List(Of String)
  17. Using sr As New StreamReader(TextBox1.Text)
  18. While sr.Peek <> -1
  19. lines.Add(sr.ReadLine())
  20. End While
  21. End Using
  22. Dim rand As Random = New Random()
  23. Dim r As Integer = rand.Next(lines.Count())
  24. TextBox2.Text = lines(r)
  25. rand = Nothing
  26. r = Nothing
  27. lines = Nothing
  28. End If
  29. End If
  30. End Sub
  31. End Class

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment