Create a New Project.
Create a large main field form. Size 700, 300 at least.
Add a label object to your form on top.
Click on the Label -> Properties window -> Text -> Change to: No Folder Selected
Bellow the label add a big textbox, size around 600, 240.
Bellow the textbox add two buttons and a checkbox.
Go to toolbox -> "Dialogs" -> Double click "Folderbrowserdialog" to add one to your program.
Click on the textbox -> Go to Properties -> Scrollbars -> choose: Vertical.
The first button (Button1) is going to be a "Choose folder" (or "Load Folder") button.
Double click on button1 and go to the sub.
Add this:
FolderBrowserDialog1.ShowDialog()
Label1.Text = FolderBrowserDialog1.SelectedPath
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
FolderBrowserDialog1.ShowDialog()
Label1.Text = FolderBrowserDialog1.SelectedPath
End Sub
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
FolderBrowserDialog1.ShowDialog()
Label1.Text = FolderBrowserDialog1.SelectedPath
End Sub
End Class
FolderBrowserDialog1.ShowDialog() - this will open the "choose folder" window.
Label1 will display the path of the chosen folder. We will now store all the files from our chosen folder to a string variable. In order to use the function that will retrieve us the file names, we need import a library to this program. Add this line at the very beginning of the program:
Imports System.IO
Outside of the button1 sub declare our string array variable:
Private bb As String()
We could have also used: Private bb() As String
Now we can add the filenames to this string inside the sub.
bb = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
Directory.Getfiles - function for retrieving filenames from a folder. It needs a folder path and defined file extension information.
"*.*" - this adds all files, "all file extensions". You could have written "*.txt" for retrieving text files only.
Code:
Imports System.IO
Public Class Form1
Private bb As String()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
FolderBrowserDialog1.ShowDialog()
Label1.Text = FolderBrowserDialog1.SelectedPath
bb = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
End Sub
End Class
Public Class Form1
Private bb As String()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
FolderBrowserDialog1.ShowDialog()
Label1.Text = FolderBrowserDialog1.SelectedPath
bb = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
End Sub
End Class
Now the "bb" variable holds all the filenames along with their folder filepath. Let's display the content of "bb" inside the textbox. We will need to add another string for this purpose. At the beginning of button1 add:
Dim d As String
At the bottom of button1 sub let's add:
TextBox1.Clear()
For Each d In bb
TextBox1.AppendText(d & vbNewLine)
NextTextBox1.Clear() - this will empty the textbox. We might want to try and open different folders during one program run, so instead of adding continuously new filenames at the bottom of the textbox, we empty it first and then add the new filenames of another folder.
For Each - is a program loop. It will keep going around until it gets at the end of the content we used in the loop. In our example that's the end of the "bb" variable,
- when it reaches "next" - one loop is over, When it reaches the end of "bb" it completes it's job.
"For each d In bb" - "for each string in a string array". Every loop will give us only one string (information) from a string array (collection of multiple strings).
TextBox1.AppendText - the "appendtext" function will add new text at the end of the textbox.
TextBox1.AppendText(d & vbNewLine) - this will add one string from "bb" and a newline to the textbox every turn. We could have used this instead:
TextBox1.Text += d & vbNewLine -> which is the same as:
TextBox1.Text = TextBox1.Text & d & vbNewLine
Code:
Imports System.IO
Public Class Form1
Private bb As String()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim d As String
Public Class Form1
Private bb As String()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim d As String
FolderBrowserDialog1.ShowDialog()
Label1.Text = FolderBrowserDialog1.SelectedPath
bb = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
TextBox1.Clear()
For Each d In bb
TextBox1.AppendText(d & vbNewLine)
Next
End Sub
End Class
Label1.Text = FolderBrowserDialog1.SelectedPath
bb = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
TextBox1.Clear()
For Each d In bb
TextBox1.AppendText(d & vbNewLine)
Next
End Sub
End Class
Start program. Check it out.
At this moment we have some code inside our button1 sub that is executed even if a person chooses "cancel" after pressing this button. This code uses the folder path value which is required. If it's not there, the program will crash. That's why we will need to change this line:
FolderBrowserDialog1.ShowDialog()
into:If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
at the bottom of the button1 sub we will close the if statement:
End If
Now a person can choose the "cancel" button at the Choose Folder prompt.
At this point, we will use the checkbox to remove the folder path from the filenames loaded inside the textbox.
Go to the design page -> Double click the checkbox -> Go to the sub.
We will add this code to the checkbox sub:
TextBox1.Clear()
If CheckBox1.Checked = True Then
For Each d In bb
d = Replace(d, FolderBrowserDialog1.SelectedPath, "")
TextBox1.text += Replace(d, "\", "") & vbNewLine
Next
End If
TextBox1.Clear() - before we get the new content in the checkbox (filenames only) we need to remove the old one
"If CheckBox1.Checked = True Then" - when we click on an empty checkbox this will become true.
d = Replace(d, FolderBrowserDialog1.SelectedPath, "") - for each string we will make changes to remove the filepath from the string array. The "Replace" function helps us removing content from arrays. It holds three arguments divided by commas: Replace(argument1, argument2, argument3)
- the first argument is the data that holds both what we want and don't want
- the second argument is what we want to remove
- the third argument is what we want to insert over what we removed.
We obviously don't want to insert anything in this example.
When loading folders you can receive two different folder path versions. One path example is:
D:\Myfolder
and another is:
D:\
The first example doesn't have a "\" sign at the end.
On the other hand after we use this line:
bb=Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
"bb" strings will receive full folder paths and filenames in both of the above examples chosen by the user."bb" string received for a file inside D:\Myfolder is going to be: D:\Myfolder\Myfile.txt
"bb" string received for a file inside D:\ is going to be: D:\Myfile.txt
So, obviously, if we remove the folder path from: D:\Myfolder\Myfile.txt we will get: \Myfile.txt as the filename. There's the extra "\" causing us problems. That's why we use the next code line below.
TextBox1.text += Replace(d, "\", "") & vbNewLine - removes "\" signs if they are present and adds a new line in the output.
Code:
Imports System.IO
Public Class Form1
Private bb As String()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim d As String
Public Class Form1
Private bb As String()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim d As String
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
Label1.Text = FolderBrowserDialog1.SelectedPath
bb = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
TextBox1.Clear()
For Each d In bb
TextBox1.AppendText(d & vbNewLine)
Next
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
TextBox1.Clear()
If CheckBox1.Checked = True Then
For Each d In bb
d = Replace(d, FolderBrowserDialog1.SelectedPath, "")
TextBox1.text += Replace(d, "\", "") & vbNewLine
Next
End If
End Sub
End Class
Label1.Text = FolderBrowserDialog1.SelectedPath
bb = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
TextBox1.Clear()
For Each d In bb
TextBox1.AppendText(d & vbNewLine)
Next
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
TextBox1.Clear()
If CheckBox1.Checked = True Then
For Each d In bb
d = Replace(d, FolderBrowserDialog1.SelectedPath, "")
TextBox1.text += Replace(d, "\", "") & vbNewLine
Next
End If
End Sub
End Class
Start program. Load a folder.and then click on the checkbox.
If a person unchecks the checkbox, he'll expect for the names to go back to normal.
That's why we need to add this inside of the if statement of checkbox1:
Else
For Each d In bb
TextBox1.Text = TextBox1.Text & d & vbNewLine
NextAt this moment a person could click on the checkbox before he actually loads a folder. This will cause the program to crash. We need to fix that.
Right below:
Private bb As String()
let's add another variable.Private c1 As Integer
"c1" is now zero.
We will add:
c1 = 1
inside of button1 after somebody chooses a folder. At the beginning of the sub of checkbox1, we will then add:
If c1 = 0 Then
CheckBox1.Checked = False
Exit Sub
End IfAs long as c1 = 0, the checkbox can't be checked. You can only check it after loading a folder.
Instead of: CheckBox1.Checked = False we could have used:
CheckBox1.CheckState = CheckState.Unchecked
Exit Sub - will not allow the sub to proceed with the code of checkbox1 sub. You can remove this and then try check and uncheck to see the difference
There should have been another button on the form. Go to design page -> double click on button 2.
Make it a "clear" button, add this:
TextBox1.clear()
Label1.Text = "No Folder Selected"
c1 = 0
CheckBox1.Checked = False
Code:
Imports System.IO
Public Class Form1
Private bb As String()
Private c1 As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim d As String
Public Class Form1
Private bb As String()
Private c1 As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim d As String
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
c1 = 1
Label1.Text = FolderBrowserDialog1.SelectedPath
bb = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
TextBox1.Clear()
For Each d In bb
TextBox1.AppendText(d & vbNewLine)
Next
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
TextBox1.Clear()
If c1 = 0 Then
CheckBox1.Checked = False
Exit Sub
End If
If CheckBox1.Checked = True Then
For Each d In bb
d = Replace(d, FolderBrowserDialog1.SelectedPath, "")
TextBox1.text += Replace(d, "\", "") & vbNewLine
Next
Else
For Each d In bb
TextBox1.Text = TextBox1.Text & d & vbNewLine
Next
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Clear()
Label1.Text = "No Folder Selected"
c1 = 0
CheckBox1.Checked = False
End Sub
End Class
c1 = 1
Label1.Text = FolderBrowserDialog1.SelectedPath
bb = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
TextBox1.Clear()
For Each d In bb
TextBox1.AppendText(d & vbNewLine)
Next
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
TextBox1.Clear()
If c1 = 0 Then
CheckBox1.Checked = False
Exit Sub
End If
If CheckBox1.Checked = True Then
For Each d In bb
d = Replace(d, FolderBrowserDialog1.SelectedPath, "")
TextBox1.text += Replace(d, "\", "") & vbNewLine
Next
Else
For Each d In bb
TextBox1.Text = TextBox1.Text & d & vbNewLine
Next
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Clear()
Label1.Text = "No Folder Selected"
c1 = 0
CheckBox1.Checked = False
End Sub
End Class
Start program.