• We just launched and are currently in beta. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

How to Create a Random Line Selector in Visual Basic

Crank

Data Analyst
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
103
Likes
149
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 800 XP
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. Private

    Sub

    Button2_Click(sender As

    Object

    , e As

    EventArgs) Handles Button2.Click
  13. If

    (Not

    TextBox1.Text = Nothing

    ) Then
  14. If

    (My.Computer.FileSystem.FileExists(TextBox1.Text)) Then
  15. Dim

    lines As

    New

    List(Of String

    )
  16. Using sr As

    New

    StreamReader(TextBox1.Text)
  17. While

    sr.Peek <> -1
  18. lines.Add(sr.ReadLine())
  19. End

    While
  20. End

    Using
  21. Dim

    rand As

    Random

    = New

    Random

    ()
  22. Dim

    r As

    Integer

    = rand.Next

    (lines.Count())
  23. TextBox2.Text = lines(r)
  24. rand = Nothing
  25. r = Nothing
  26. lines = Nothing
  27. End

    If
  28. End

    If
  29. End

    Sub
  30. 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.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,497

347,618

347,626

Top