Crank
Data Analyst
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...
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.
Project Complete!
That's it! Below is the full source code and download to the project files.
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
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...
- Private
Sub
Button1_Click(sender As
Object
, e As
EventArgs) Handles Button1.Click
- Dim
fo As
New
OpenFileDialog
- fo.Filter = "Text Files|*.txt"
- fo.FilterIndex = 1
- fo.ShowDialog()
- If
(Not
fo.FileName = Nothing
) Then
- TextBox1.Text = fo.FileName
- End
If
- 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.
- Private
Sub
Button2_Click(sender As
Object
, e As
EventArgs) Handles Button2.Click
- If
(Not
TextBox1.Text = Nothing
) Then
- If
(My.Computer.FileSystem.FileExists(TextBox1.Text)) Then
- Dim
lines As
New
List(Of String
)
- Using sr As
New
StreamReader(TextBox1.Text)
- While
sr.Peek <> -1
- lines.Add(sr.ReadLine())
- End
While
- End
Using
- Dim
rand As
Random
= New
Random
()
- Dim
r As
Integer
= rand.Next
(lines.Count())
- TextBox2.Text = lines(r)
- rand = Nothing
- r = Nothing
- lines = Nothing
- End
If
- End
If
- End
Sub
Project Complete!
That's it! Below is the full source code and download to the project files.
- Imports System.IO
- Public
Class Form1
- Private
Sub
Button1_Click(sender As
Object
, e As
EventArgs) Handles Button1.Click
- Dim
fo As
New
OpenFileDialog
- fo.Filter = "Text Files|*.txt"
- fo.FilterIndex = 1
- fo.ShowDialog()
- If
(Not
fo.FileName = Nothing
) Then
- TextBox1.Text = fo.FileName
- End
If
- End
Sub
- Private
Sub
Button2_Click(sender As
Object
, e As
EventArgs) Handles Button2.Click
- If
(Not
TextBox1.Text = Nothing
) Then
- If
(My.Computer.FileSystem.FileExists(TextBox1.Text)) Then
- Dim
lines As
New
List(Of String
)
- Using sr As
New
StreamReader(TextBox1.Text)
- While
sr.Peek <> -1
- lines.Add(sr.ReadLine())
- End
While
- End
Using
- Dim
rand As
Random
= New
Random
()
- Dim
r As
Integer
= rand.Next
(lines.Count())
- TextBox2.Text = lines(r)
- rand = Nothing
- r = Nothing
- lines = Nothing
- End
If
- End
If
- End
Sub
- 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.