We will add more options to the previous program from example 11.
So far it loads a folder and carries the filenames into the combobox. It only takes the filenames with the file extensions chosen.
Let's add a tab page and the ability for the user to load the text from the file in the combobox into a chosen tabpage.
You should have a load button on the top left of your form. A label on the left of the button. A combobox right below the button and a "Folderbrowsedialog" added. We will need a big main field form for the rest of the program - size around 720, 450.
Go to "Toolbox" -> "All windows forms" -> Add a "Groupbox" below the combobox in the main field form.
Inside the "Groupbox" insert at least five "RadioButtons" from the toolbox. Resize the "Groupbox" to fit the buttons inside. You can add more, 10 would be quite enough.
The "groupbox" connects the "radio buttons" together. When you click on one radio button, it will become checked, and all the others will become unchecked. Which ones become unchecked is determined by the groupbox. If you had no groupbox, then only one radio button would be checked at any moment.
Click on the first RadioButton -> go to thr properties window -> Checked: set to "True"
The first radio button will be turned on when the program starts.
Below the radiobuttons, outside of the groupbox, add another button. You can set the text to "Start" or "Go".
Below the label and on the right of all the radiobuttons add a Tabcontrol.
"Toolbox" -> Go to "Windows All Forms" -> "Tabcontrol"
It should have one tabpage inside right after adding. It's important to now the difference. The tabcontrol is a form that holds tabpages. You cannot write directly text on the tab page but you can insert objects into it.
In order for us to write inside a tabpage, we will add textboxes inside of them.
Since we will start the program with one tab page already open let's a textbox in it as well.
Add a textbox from the toolbox into the tabpage. Choose multiline, go to properties window -> "Dock" -> Open the small dropdown menu and choose the "centering configuration" or just write "Fill" in the small box and press enter. The textbox will now resize to fit the whole size of the tab page.
If you ever need to remove it from the field form, click on the textbox and press the "delete" key.
Choose the Groupbox -> Properties -> Text: Choose A Page
Radiobuttons -> Properies -> Text: Tabpage1, Tabpage2, Tabpage3 and so on...
Starting code is the same one present at the end of example 11.
Code:
Public Class Form1
Private f1 As String()
Label1.Text = FolderBrowserDialog1.SelectedPath
f1 = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
End If
End Sub
End Class
If a person loads one folder after the other, the program will keep adding files at the bottom of the combobox while retaining the filenames of the previous folder. That's why need to add this at the beginning of button1:
ComboBox1.Items.Clear()
All the operations of choosing a file from the combobox, finding out which radio button is checked, creating a new tab page, inserting a textbox in the new tab page and inserting the content of the file in the textbox -> all of that can be done after click the second button we have, the "Go" button. So we can write the code of all of that inside that button and just use the names of the other objects.
Designer page -> Double click on button2 to create sub.
Every radio button represents the choice of one tabpage. Only "tabpage1" is loaded at the start, all others will have to be created.
When we create a tabpage we will call it "TabPage(number)".
(number) will change depending on which radio button is currently checked.
First we add an integer for these numbers below line: Private g1 As String() = {".txt", ".pdf", ".doc"}. Add:
Private g As Integer
Inside "button 2" we then add:
If RadioButton1.Checked Then
-> add more elseif-s if you have more buttons. We will shorten this later on.
Below the previous code we will add this:
Dim NewTab As New TabPage -> "NewTab" is just a name for a variable, you can change it. "NewTab" is declared to be a TabPage, which in this line represents a data type of an object.
-> with the following two lines we determine the "name" and "text" of the new tabpage. We could have determined more properties if we wanted to or we could have determined none. If you don't specify a property, it will be set as either default or completely empty.
TabControl1.TabPages.Add(NewTab) - "TabControl1" is the name of the parent form that holds tab pages. "TabPages" here are a set of objects that TabControl contains. It can also have other ones. "Add" is a function that is generally used in visual basic for adding a new object.
TabControl1.TabPages - is a location, and "add" is a function applied to that location. There are other functions that can be used. We will be add a NewTab which holds our determined properties.
You can always declare more variables TabPage data type inside your program and set different properties under them, then call the one that you require later on.
The "add" options adds another page at the very end of your tab pages. It always adds after the last tab page index.
We proceed by adding textboxes inside each new tabpage. Add below the previous code:
-> the same we did for a new tab page. We declare it and then specify some properties that we need different than default.
NewTab.Controls.Add(textbox) - "NewTab" is a location created with our previously created TabPage variable. If you change the name of the variable, you will have to change this too. "Controls" is a set of all objects present inside the tabpage. This defines that we will be adding content to our dynamically created tabpage - an element produced inside the code rather than pre-determined during program design.
So far we choose a tabpage number, created a new tabpage inside the tabcontrol and added a new textbox inside the new tabpage. Now we have to add content to the new textbox. This means that we need to store the content of a file into a streamreader variable and then write it out from the variable into our textbox.
Right below our previous declaration: Private g As Integer add:
Private a1 As StreamReader
Back to the bottom of button2 sub. Add:
ComboBox1.SelectedItem - you click on the arrow of the combobox, the dropdown menu appears and you select an item from the menu. The selected item's text stays inside the text-box of the combobox. "ComboBox1.SelectedItem" is have we point to that text.
In this specific program, the value of the item is also a filepath we use the load a text from a file into our "a1" variable.
a1.ReadToEnd() - read all the content from "a1" to the end
Code:
Public Class Form1
Private f1 As String()
Private g1 As String() = {".txt", ".pdf", ".docx"}
Private g As Integer
Private a1 As StreamReader
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ComboBox1.Items.Clear()
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
Label1.Text = FolderBrowserDialog1.SelectedPath
f1 = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
For Each i In f1
For i2 = 0 To g1.GetUpperBound(0)
If i.EndsWith(g1(i2)) Then
ComboBox1.Items.Add(i)
End If
Next
Next
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If RadioButton1.Checked Then
g = 1
ElseIf RadioButton2.Checked Then
g = 2
ElseIf RadioButton3.Checked Then
g = 3
ElseIf RadioButton4.Checked Then
g = 4
ElseIf RadioButton5.Checked Then
g = 5
End If
Dim NewTab As New TabPage
NewTab.Name = "TabPage" & g
NewTab.Text = "TabPage" & g
TabControl1.TabPages.Add(NewTab)
Dim textbox As New TextBox
textbox.Name = "Textbox" & g
textbox.Multiline = True
textbox.Dock = DockStyle.Fill
NewTab.Controls.Add(textbox)
a1 = File.OpenText(ComboBox1.SelectedItem)
NewTab.Controls("Textbox" & g).Text = a1.ReadToEnd()
End Sub
End Class
Currently the code will keep adding a new tab page, every time we click the "Go" button. If we don't choose a filename properly, clicking on the "Go" button will make everything crash.
We need to shorten some of this code and make some more changes to improve the program.
I'll end this in the next post. Next Example