Here we continue the small program started at example 11 and modified in example 12.
I will first only show the code of button2. Full program code is going to be on the bottom of this post.
Code of button2:
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
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
Below the "End If" line add this:
TabControl1.Controls.Remove(TabControl1.Controls("Tabpage" & g))
- If somebody chooses the same tab page multiple times (same radiobutton then clicking "Go"), the old tab page will get deleted now and a new one will be created on it's place.
I tried to shorten this line, but other versions didn't work. This won't work:
Remove(TabControl1.Controls("Tabpage" & g))
or
Controls.Remove(TabControl1.Controls("Tabpage" & g))
or
TabControl1.Controls("Tabpage" & g).Remove()
Below this line: TabControl1.TabPages.Add(NewTab) add this:
TabControl1.SelectedTab = NewTab
- Now the program will directly select the newly created tab, instead of just creating it while the focus is kept on the previously chosen tab.
We will also change this 2 lines:
a1 = File.OpenText(ComboBox1.SelectedItem)
NewTab.Controls("Textbox" & g).Text = a1.ReadToEnd()into:
Try
a1 = File.OpenText(ComboBox1.SelectedItem)
NewTab.Controls("Textbox" & g).Text = a1.ReadToEnd()
Catch
MsgBox("Error caught" & vbNewLine & "Please choose a good file address")
End Try"Try" follows a working program code to see if there will be any program error occurrences. It disallows the error to stop (crash) the program.
"Catch" is what we tell our program to do, if an error was found, in this case we will print a message out to the user.
In this program an error happens if the combobox selection of a filepath is not good (empty, partially deleted/changed or simply a not working file address).
It's important to remember how the "Try" function work. If you had 10 operations wrapped by a "Try" function, and if only the tenth operation was faulty, then "Try" would do all of the previous operations. It would only skip the last one from going through and crashing the system.
It doesn't "first check all of the operations" for errors and then - "does them only if there was no error found" or "it doesn't do any of them at all". That's not how it works.
Current code for button2:
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
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
TabControl1.Controls.Remove(TabControl1.Controls("Tabpage" & g))
Dim NewTab As New TabPage
NewTab.Name = "TabPage" & g
NewTab.Text = "TabPage" & g
TabControl1.TabPages.Add(NewTab)
TabControl1.SelectedTab = NewTab
Dim textbox As New TextBox
textbox.Name = "Textbox" & g
textbox.Multiline = True
textbox.Dock = DockStyle.Fill
NewTab.Controls.Add(textbox)
Try
a1 = File.OpenText(ComboBox1.SelectedItem)
NewTab.Controls("Textbox" & g).Text = a1.ReadToEnd()
Catch
MsgBox("Error caught" & vbNewLine & "Please choose a good file address")
End Try
End Sub
End Class
The first part of the code of button2 checks which radio button is checked. That piece of code has repetition inside which should be shortened.
Remove the complete first "if" statement in button2 and add this instead:
For Each RadioButton In GroupBox1.Controls
If RadioButton.Checked = True Then
g = Replace(RadioButton.name, "RadioButton", "")
Exit For
End If
Next
For Each RadioButton In GroupBox1.Controls - this "for" loop looks for a radiobutton object inside our groupbox element.
If RadioButton.Checked = True Then - only one radiobutton can be checked. When we find that button, we do the following line of code and immediately exit the loop.
g = Replace(RadioButton.name, "RadioButton", "") - we create our "g" number by deleting the "RadioButton" from name of the object and taking the number out. We can do that in this program since "RadioButton1" determines the creation of "TabPage1", "RadioButton2" determines "TabPage2" and so on.Otherwise, you'd have to adjust by either adding or reducing a number from the "Replace(...)" bit.
Sorting the tab pages
First, we will add an integer variable right below this line: Private a1 As StreamReader, add:
Private i3 As Integer
Below this line:
TabControl1.Controls.Remove(TabControl1.Controls("Tabpage" & g))
add:
i3 = 0
While i3 < TabControl1.TabPages.Count
If g < Replace(TabControl1.TabPages(i3).Text, "TabPage", "") Then
Exit While
End If
i3 = i3 + 1
End WhileWhile i3 < TabControl1.TabPages.Count - the "while" loop will go up to the number of currently present tab pages. "i3" is going to be used as the index number for inserting new tabs amongst present ones.
If g < Replace() Then - "g" is the number of the tab page that we want.
TabControl1.TabPages(i3).Text - is the text of a currently present tab page checked by index. Imagine there were three of them present: "TabPage1", "TabPage4" and "TabPage5". Their current indexes as tabpages would be: 0, 1 and 2. The "while" loop changes the indexes from 0 to current number of tab pages.
Replace() - we use the "Replace" function to strip the text of the tab pages from this: "TabPages" and only leave their number. The number that is left is then compared to "g".
Why do we compare the number of the tab page that we want ("g") with the number of each of the currently present tab pages? We do that to be able to insert our desired new tab page in it's proper numerical position. In this case, we do that by creating an index value ("i3") for our new tab page with a "while" loop..
Example:
"TabPage1" is already present. It's index is 0.
We choose to add: "Tabpage2". The "while" loop doesn't get stopped, and it gives us i3 = 1 at the end.
So we insert "Tabpage2" at index 1, which means after "TabPage1"
Then we choose to add "Tabpage5". The number in the text of "TabPage5" is higher than both "TabPage1" and "TabPage2" - therefore - the "while" loop doesn't get stopped again during both of those comparisons, and it gives us i3 = 2. So, "TabPage5" is inserted at the very end, at index 2.
We then choose to add: "TabPage4". The number in the text of "TabPage4" is higher than both "TabPage1" and "Tabpage2", so the "while" loop makes those two comparisons and raises i3 to 2. Then the condition of the "if" statement: g < Replace(...) kicks in and stops the loop, since the "TabPage4" is smaller than "TabPage5". This way i3 is stopped at 2, instead of continuing to go up to 3 and we insert "TabPage4" at index 2, right between "Tabpage2" and "Tabpage5"
You can add a message box right below the previous code (below the "end while"):
MsgBox("i3 = " & i3)
and check this example out while the program is working.
The last thing we have to do is replace the old "add" function we were using to create new tab pages with an "insert" function, that operates on index numbers. Take this line of code:
TabControl1.TabPages.Add(NewTab)
and replace it with:
TabControl1.TabPages.Insert(i3, NewTab)
Final code:
Imports System.IO
Public Class Form1
Private f1 As String()
Private g1 As String() = {".txt", ".pdf", ".docx"}
Private g As Integer
Private a1 As StreamReader
Private i3 As Integer
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
For Each RadioButton In GroupBox1.Controls
If RadioButton.Checked = True Then
g = Replace(RadioButton.name, "RadioButton", "")
Exit For
End If
Next
TabControl1.Controls.Remove(TabControl1.Controls("Tabpage" & g))
i3 = 0
While i3 < TabControl1.TabPages.Count
If g < Replace(TabControl1.TabPages(i3).Text, "TabPage", "") Then
Exit While
End If
i3 = i3 + 1
End While
Dim NewTab As New TabPage
NewTab.Name = "TabPage" & g
NewTab.Text = "TabPage" & g
TabControl1.TabPages.Insert(i3, NewTab)
TabControl1.SelectedTab = NewTab
Dim textbox As New TextBox
textbox.Name = "Textbox" & g
textbox.Multiline = True
textbox.Dock = DockStyle.Fill
NewTab.Controls.Add(textbox)
Try
a1 = File.OpenText(ComboBox1.SelectedItem)
NewTab.Controls("Textbox" & g).Text = a1.ReadToEnd()
Catch
MsgBox("Error caught" & vbNewLine & "Please choose a good file address")
End Try
End Sub
End Class
Public Class Form1
Private f1 As String()
Private g1 As String() = {".txt", ".pdf", ".docx"}
Private g As Integer
Private a1 As StreamReader
Private i3 As Integer
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
For Each RadioButton In GroupBox1.Controls
If RadioButton.Checked = True Then
g = Replace(RadioButton.name, "RadioButton", "")
Exit For
End If
Next
TabControl1.Controls.Remove(TabControl1.Controls("Tabpage" & g))
i3 = 0
While i3 < TabControl1.TabPages.Count
If g < Replace(TabControl1.TabPages(i3).Text, "TabPage", "") Then
Exit While
End If
i3 = i3 + 1
End While
Dim NewTab As New TabPage
NewTab.Name = "TabPage" & g
NewTab.Text = "TabPage" & g
TabControl1.TabPages.Insert(i3, NewTab)
TabControl1.SelectedTab = NewTab
Dim textbox As New TextBox
textbox.Name = "Textbox" & g
textbox.Multiline = True
textbox.Dock = DockStyle.Fill
NewTab.Controls.Add(textbox)
Try
a1 = File.OpenText(ComboBox1.SelectedItem)
NewTab.Controls("Textbox" & g).Text = a1.ReadToEnd()
Catch
MsgBox("Error caught" & vbNewLine & "Please choose a good file address")
End Try
End Sub
End Class