Home » June 2013
Visual Basic 17 - Using A Random Number Generator

Create a new project

Add a button on top of the form, change text to: Start Timer
Below the button add 3 labels, 3 textboxes and 3 checkboxes.
Also add a Timer to the form.

We will use the timer in this program to start the generation of random numbers and add them randomly to our objects.

Double click on Timer1 on the design form, add this inside the sub:

If Timer1.Enabled = True Then
Timer1.Enabled = False
Button1.Text = "Start Timer"
Exit Sub
End If
Timer1.Enabled = True
Button1.Text = "Stop"

-> first click on the button starts the timer, the following click will stop it

Double click on the timer, insert this in the sub:
Dim a1 As New Random
Dim b1 As Integer
Dim b2 As Integer

b1 = a1.Next(1, 101)
b2 = a1.Next(1, 4)

-> "a1" is here the name of our random generator
a1.Next(1, 101)  - we use "a1" to generate numbers from 1 to 100
We will store the generated numbers inside the "b1" and "b2" variables.

Following this code we will add inside the same sub:
If b1 > 0 And b1 < 34 Then
ElseIf b1 > 33 And b1 < 67 Then
ElseIf b1 > 66 And b1 < 101 Then
End If

Below this line If b1 > 0 And b1 < 34 Then add:
If b2 = 1 Then
Label1.Text = b1
ElseIf b2 = 2 Then
TextBox1.Text = b1
ElseIf b2 = 3 Then
CheckBox1.Text = b1
End If

-> b2 will determine where b1 is gonna be inserted as text.

Copy the previous code below this line too ElseIf b1 > 33 And b1 < 67 Then.  Then change the number "1" from the object names: Label1, TextBox1 and CheckBox1  to 2.
Copy the code below this line as well: ElseIf b1 > 66 And b1 < 101 Then
Change the numbers from the object names to 3.

Code:

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If Timer1.Enabled = True Then
    Timer1.Enabled = False
    Button1.Text = "Start Timer"
    Exit Sub
    End If

    Timer1.Enabled = True
    Button1.Text = "Stop"
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim a1 As New Random
    Dim b1 As Integer
    Dim b2 As Integer

    b1 = a1.Next(1, 101)
    b2 = a1.Next(1, 4)

    If b1 > 0 And b1 < 34 Then
    If b2 = 1 Then
    Label1.Text = b1
    ElseIf b2 = 2 Then
    TextBox1.Text = b1
    ElseIf b2 = 3 Then
    CheckBox1.Text = b1
    End If

    ElseIf b1 > 33 And b1 < 67 Then
    If b2 = 1 Then
    Label2.Text = b1
    ElseIf b2 = 2 Then
    TextBox2.Text = b1
    ElseIf b2 = 3 Then
    CheckBox2.Text = b1
    End If

    ElseIf b1 > 66 And b1 < 101 Then
    If b2 = 1 Then
    Label3.Text = b1
    ElseIf b2 = 2 Then
    TextBox3.Text = b1
    ElseIf b2 = 3 Then
    CheckBox3.Text = b1
    End If
    End If

    End Sub
    End Class


Start program.

We can add more changes to the current program.
Add another button next to the previous one, change text to: Start Timer2
Add another Timer too.

Changing randomly the background of our objects

Object_Name.BackColor = Color.     -> at this point inside the code you are offered a menu with predefined colors. Instead of using those, we will use custom colors and randomize their number parameters.
Passing a custom color from the RGB color model to the background is usually done like this:
Object_Name.BackColor = Color.FromArgb(number1, number2, number3)

-> "number1", "number2" and "number3" are numbers from 0 to 255. We will apply our random code here:
a1.next(0, 255)

I just copied all the previous code of the program and adapted it to the new button and timer. Then I have inserted the new background changes that will be happening. You can keep adding variety to the objects like this.

Final code:

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If Timer1.Enabled = True Then
    Timer1.Enabled = False
    Button1.Text = "Start Timer"
    Exit Sub
    End If

    Timer1.Enabled = True
    Button1.Text = "Stop"
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim a1 As New Random
    Dim b1 As Integer
    Dim b2 As Integer

    b1 = a1.Next(1, 101)
    b2 = a1.Next(1, 4)

    If b1 > 0 And b1 < 34 Then
    If b2 = 1 Then
    Label1.Text = b1
    ElseIf b2 = 2 Then
    TextBox1.Text = b1
    ElseIf b2 = 3 Then
    CheckBox1.Text = b1
    End If

    ElseIf b1 > 33 And b1 < 67 Then
    If b2 = 1 Then
    Label2.Text = b1
    ElseIf b2 = 2 Then
    TextBox2.Text = b1
    ElseIf b2 = 3 Then
    CheckBox2.Text = b1
    End If

    ElseIf b1 > 66 And b1 < 101 Then
    If b2 = 1 Then
    Label3.Text = b1
    ElseIf b2 = 2 Then
    TextBox3.Text = b1
    ElseIf b2 = 3 Then
    CheckBox3.Text = b1
    End If
    End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    If Timer2.Enabled = True Then
    Timer2.Enabled = False
    Button2.Text = "Start Timer2"
    Exit Sub
    End If

    Timer2.Enabled = True
    Button2.Text = "Stop"
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    Dim a1 As New Random
    Dim b1 As Integer
    Dim b2 As Integer

    b1 = a1.Next(1, 101)
    b2 = a1.Next(1, 4)

    If b1 > 0 And b1 < 34 Then
    If b2 = 1 Then
    Label1.BackColor = Color.FromArgb(a1.Next(0, 255), a1.Next(0, 255), a1.Next(0, 255))
    ElseIf b2 = 2 Then
    TextBox1.BackColor = Color.FromArgb(a1.Next(0, 255), a1.Next(0, 255), a1.Next(0, 255))
    ElseIf b2 = 3 Then
    CheckBox1.BackColor = Color.FromArgb(a1.Next(0, 255), a1.Next(0, 255), a1.Next(0, 255))
    If CheckBox1.Checked = True Then
    CheckBox1.Checked = False
    Exit Sub
    End If
    CheckBox1.Checked = True
    End If

    ElseIf b1 > 33 And b1 < 67 Then
    If b2 = 1 Then
    Label2.BackColor = Color.FromArgb(a1.Next(0, 255), a1.Next(0, 255), a1.Next(0, 255))
    ElseIf b2 = 2 Then
    TextBox2.BackColor = Color.FromArgb(a1.Next(0, 255), a1.Next(0, 255), a1.Next(0, 255))
    ElseIf b2 = 3 Then
    CheckBox2.BackColor = Color.FromArgb(a1.Next(0, 255), a1.Next(0, 255), a1.Next(0, 255))
    If CheckBox2.Checked = True Then
    CheckBox2.Checked = False
    Exit Sub
    End If
    CheckBox2.Checked = True
    End If

    ElseIf b1 > 66 And b1 < 101 Then
    If b2 = 1 Then
    Label3.BackColor = Color.FromArgb(a1.Next(0, 255), a1.Next(0, 255), a1.Next(0, 255))
    ElseIf b2 = 2 Then
    TextBox3.BackColor = Color.FromArgb(a1.Next(0, 255), a1.Next(0, 255), a1.Next(0, 255))
    ElseIf b2 = 3 Then
    CheckBox3.BackColor = Color.FromArgb(a1.Next(0, 255), a1.Next(0, 255), a1.Next(0, 255))
    If CheckBox3.Checked = True Then
    CheckBox3.Checked = False
    Exit Sub
    End If
    CheckBox3.Checked = True
    End If
    End If
    End Sub

    End Class


Start the program.

Obviously, the whole program could be placed under a single button and timer. Then you won't be able to apply different changes with different buttons.


Visual Basic 16 - Adding Objects Dynamically To Your Form, Listbox

Create a new project.

Size of main form around: 660, 350
Add a listbox to the top left part of your form, size of the listbox: 160, 130
Below the listbox add a button, change text to: Remove
Below the button add a textbox, multiline yes, size around: 220, 70

On the top right of your form add a label, change text to: Add TextBox:
Below the label add 3 radio buttons, text of the first radio button: Bottom
second radio button text: Right
third radio button text: Custom
Below the radio buttons add a button, text: Create
Below this add another label.

Add a Timer to your program.

Click on the listbox1 -> Properties -> Items ->  Click on the 3 dots -> Add: TextBox1 on the list -> OK
Click on the "Bottom" radio button, go to properties ->  Checked: True
Click on Timer1 -> Properties -> Enabled: True

The program will allow adding textboxes on the main form. Each new textbox will be added to the listbox as it gets created.

First we will add the code that creates new textboxes. Every new textbox will have a name with a number higher than the previous one. We will need a variable for that. Right below "Public Class Form1" add:
Private mm As Integer = 2

-> we start at 2 because there's already one textbox on the main form.

Double click the "Create" button. Add this inside the sub:
Dim bbt As New TextBox
bbt.Name = "TextBox" & mm
bbt.Multiline = True
bbt.Height = TextBox1.Height
bbt.Width = TextBox1.Width

-> every new textbox will have this properties. Height and width are going to be the same as the textbox that is already present on the main form. We will need to specify a location for the new textbox, otherwise the coordinates will be 0,0 - which is not what we want.
The location is going to be determined depending on which radio button is checked.

If we choose the "bottom" option, than we will want for the new textbox to appear at the very bottom of all the previously created textboxes. We need a variable to help us always put our new textbox on the very bottom.
The same goes for the "right" option. So let's declare 2 new variables right below Private mm As Integer = 2

Private mm1 As Integer = 1
Private mm2 As Integer = 1

Inside the "create" button sub, below the previous code, add this:
If RadioButton1.Checked Then
bbt.Location = New Point(TextBox1.Location.X, TextBox1.Location.Y + mm1 * (TextBox1.Height + 10))
mm1 = mm1 + 1
End If

-> if the "bottom" radio button is checked, then the location of our new textbox is going to be:
TextBox1.Location.X
x is the horizontal pixel distance on your screen, it's going to be the same as textbox1 distance.
TextBox1.Location.Y + mm1 * (TextBox1.Height + 10) - y determines the vertical location on the screen, we start at the main vertical location of textbox1 and add the height of textbox1 and then 10 more pixels for the distance between the textboxes. Newly made textboxes will raise the "mm1" number which will ensure we always add the following on the bottom.

The same thing goes for the "right" option. Below the previous code add:
If RadioButton2.Checked Then
bbt.Location = New Point(TextBox1.Location.X + mm2 * (TextBox1.Width + 10), TextBox1.Location.Y)
mm2 = mm2 + 1
End If

-> if the "right" radio button is checked, add a new textbox on the right of the main starting textbox.

Below the previous code, still inside the sub of the "create" button, add:
Controls.Add(bbt)
ListBox1.Items.Add("TextBox" & mm)
mm = mm + 1

Controls.Add(bbt)  - adds a new textbox inside the form. it gives it all the properties specified for "bbt".
ListBox1.Items.Add("TextBox" & mm) - this will add a textbox item in the listbox.
mm = mm + 1 - every new textbox will have a number higher by 1

The first 2 options should work now inside the program.

Code:

    Public Class Form1

    Private mm As Integer = 2
    Private mm1 As Integer = 1
    Private mm2 As Integer = 1

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim bbt As New TextBox
    bbt.Name = "TextBox" & mm
    bbt.Multiline = True
    bbt.Height = TextBox1.Height
    bbt.Width = TextBox1.Width

    If RadioButton1.Checked Then
    bbt.Location = New Point(TextBox1.Location.X, TextBox1.Location.Y + mm1 * (TextBox1.Height + 10))
    mm1 = mm1 + 1
    End If

    If RadioButton2.Checked Then
    bbt.Location = New Point(TextBox1.Location.X + mm2 * (TextBox1.Width + 10), TextBox1.Location.Y)
    mm2 = mm2 + 1
    End If

    Controls.Add(bbt)
    ListBox1.Items.Add("TextBox" & mm)
    mm = mm + 1

    End Sub
    End Class


The "custom" option will wait for the user to make a click on the form which will decide the location of the new textbox. This means that we will have to take the coordinates of the cursor right after that click.

We have to declare a new variable that will store the coordinates of the mouse. At the start of the program, below the declarations of the other variables add this:
Private pp As New Point

Add this sub inside your code, below the previous sub:
Sub create(ByVal sender As System.Object, ByVal e As System.EventArgs)
pp = PointToClient(MousePosition)
RemoveHandler Click, AddressOf create
Button2_Click(Nothing, Nothing)
End Sub

->  the sub will collect the mouse coordinates inside the "pp" variable. It doesn't expect any events at the moment. We will add a "click" event to this sub inside of the third "custom" option in the previous sub, so that the following mouse click starts this sub.
We directly add the function to remove the "click" as event after we have received what we want.
Button2_Click(Nothing, Nothing) - we call button2 from here so that the program goes to the three code lines at the bottom. We will have to add a condition to the third "if" statement inside button2 so that it is not applied again on the following run through.

Inside the button2 sub, below this line:  
bbt.Width = TextBox1.Width
add this:
bbt.Location = pp

-> if the "bottom" or "right" options are selected, the "bbt.location" value will be changed again.

Below the second "if" statement add the third option:
If RadioButton3.Checked And Label2.Text = "" Then
AddHandler Click, AddressOf create
Label2.Text = "! Select Location !"
Exit Sub
End If

-> if the third radio button is selected, this will then add a "click" event to the "create" sub. This happens after the "create" button is pressed. We avoid the creation of the textbox at the bottom of button2 by exiting.
The text of the label2 also gets changed here.We use that to our favor, after the "create" sub takes the coordinates from the cursor, it returns the program to button2 which avoids the third "if" statement since the label2 has been changed.

Below the 3 code lines at the bottom of button2 add this:
Label2.Text = ""

Also, go to the design page, select a good location on your form for label2 -> go to properties -> erase all text from the text property. Now the text from label2 will only appear when we are selecting a custom location for our textbox.

At this moment, if you create objects that go over the borders of the main program form, there won't be any scrollbars appearing on the form so that you can actually scroll and see those objects. You have to go to the design page -> click on the blue header of the main form in order to select it -> Properties -> turn "AutoScroll" to true. They should appear now.

We will add the ability to select a textbox inside the listbox object (highlight it) and automatically change the borders of the same textbox inside our program. This way we visually know the location of the textbox we have selected in the listbox. Double click on the listbox, add inside the sub:
For Each i In Controls.OfType(Of TextBox)()
If i.Name = ListBox1.SelectedItem Then
i.BorderStyle = BorderStyle.FixedSingle
i.Focus()
Else
i.BorderStyle = BorderStyle.Fixed3D
End If
Next

-> The starting event of the listbox should be "SelectedIndexChanged", so the code of the listbox is started every time we select an item in it.

For Each i In Controls.OfType(Of TextBox)() - this will search through all the textboxes inside the whole program. The loop automatically stops when it reaches the last textbox.
If i.Name = ListBox1.SelectedItem Then - if the name of the currently selected textbox in the "for" loop is equal to the name we have selected inside the listbox, then change the border style of that textbox and send the text cursor (the blinking line) inside the textbox. Under "else" all other textbox receive the default border style. This will all make the selected textbox appear as highlighted.

The opposite of the above operation would be: selecting a texbox inside the program form, and the selected item inside the listbox changing accordingly. To find out which textbox is currently focused on we will use the timer. Double click the Timer1 in the design page and add this inside the sub:
For Each i In Controls.OfType(Of TextBox)()
If i.Focused Then
ListBox1.SelectedItem = i.Name
End If
Next

-> when the timer locates the textbox that is focused on, it will take it's name and change the currently selected item in the listbox with this name.

We will use the button below the listbox to remove items from the list and the textboxes present inside the program. Double click on the "Remove" button and add this:
For Each i In Controls.OfType(Of TextBox)()
If i.Name = ListBox1.SelectedItem Then
Controls.Remove(Controls(ListBox1.SelectedItem))
End If
Next
ListBox1.Items.Remove(ListBox1.SelectedItem)

-> the "for" loop will take the name of the selected item in the listbox and find the desired textbox with it.
Controls.Remove(Controls(ListBox1.SelectedItem))  - this is how we remove a control from the form. We need to specify the name of the object inside the second parentheses to do that.
ListBox1.Items.Remove(ListBox1.SelectedItem) - this will remove the selected item from the listbox list. Putting the "for" loop below this line wouldn't give us what we want. The selected item would be erased and the loop wouldn't be able to use it to erase the object from the form.

Final code:

    Public Class Form1

    Private mm As Integer = 2
    Private mm1 As Integer = 1
    Private mm2 As Integer = 1
    Private pp As New Point

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim bbt As New TextBox
    bbt.Name = "TextBox" & mm
    bbt.Multiline = True
    bbt.Height = TextBox1.Height
    bbt.Width = TextBox1.Width
    bbt.Location = pp

    If RadioButton1.Checked Then
    bbt.Location = New Point(TextBox1.Location.X, TextBox1.Location.Y + mm1 * (TextBox1.Height + 10))
    mm1 = mm1 + 1
    End If

    If RadioButton2.Checked Then
    bbt.Location = New Point(TextBox1.Location.X + mm2 * (TextBox1.Width + 10), TextBox1.Location.Y)
    mm2 = mm2 + 1
    End If

    If RadioButton3.Checked And Label2.Text = "" Then
    AddHandler Click, AddressOf create
    Label2.Text = "! Select Location !"
    Exit Sub
    End If

    Controls.Add(bbt)
    ListBox1.Items.Add("TextBox" & mm)
    mm = mm + 1
    Label2.Text = ""
    End Sub


    Sub create(ByVal sender As System.Object, ByVal e As System.EventArgs)
    pp = PointToClient(MousePosition)
    RemoveHandler Click, AddressOf create
    Button2_Click(Nothing, Nothing)
    End Sub

     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    For Each i In Controls.OfType(Of TextBox)()
    If i.Name = ListBox1.SelectedItem Then
    i.BorderStyle = BorderStyle.FixedSingle
    i.Focus()
    Else
    i.BorderStyle = BorderStyle.Fixed3D
    End If
    Next
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    For Each i In Controls.OfType(Of TextBox)()
    If i.Focused Then
    ListBox1.SelectedItem = i.Name
    End If
    Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    For Each i In Controls.OfType(Of TextBox)()
    If i.Name = ListBox1.SelectedItem Then
    Controls.Remove(Controls(ListBox1.SelectedItem))
    End If
    Next
    ListBox1.Items.Remove(ListBox1.SelectedItem)
    End Sub
    End Class


Start program.

Visual Basic 15 - Getting Cursor Position, Add-Remove An Event


We'll continue here the program started in example 14.

Currently, when we turn the first checkbox on and press the "capture" button, we can't click on the "cancel" properly since the timer is constantly re-focusing back on the "capture" button. We should be able to click on the "cancel" button normally.

Let's create a whole new sub for button2. Below all subs in the coding page, add this:
Private Sub Button2_Mouse(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.MouseMove, Button2.MouseLeave
End Sub

-> the name of the sub is gonna be: Button2_Mouse. The arguments it holds (the variables) can be copied from other subs. At the very end we add two events: MouseMove and MouseLeave

Inside the sub we will add this:

If Button2.Focused = True Then
Button1.Focus()
Label1.Text = "! Scroll with your mouse and then press Space !"
AddHandler Button2.MouseMove, AddressOf Button2_MouseMove
Exit Sub
End If
        
If Button2.Visible = True Then
Button2.Focus()
Label1.Text = "Cancel button selected"
RemoveHandler Button2.MouseMove, AddressOf Button2_MouseMove
End If

This sub registers two events MouseMove - sub gets triggered when you cause mouse movement over the object, while MouseLeave - triggers the code of the sub when you leave the object with your mouse cursor.

First we check the second "if" statement, since it's the first operation that gets started. 
What we wanted to achieve: after we press the "capture" button, we scroll over onto the "cancel" button, we trigger the MouseMove event and start the sub operations: the focus will get transferred to button2 and label text will be changed. We then remove the MouseMove event so that it no longer starts the sub. The only event that will now trigger it again is leaving button2 with your mouse cursor. 

The first "if" statement -> if you decide not to press the "cancel" button and move away, then you'll return the focus on button1, label is reset and we add the MouseMove event back to the sub again.

The second "if" statement might appear as unnecessary but if it's removed (and the code inside is kept) then after you click on the "cancel" button, the program will return to this sub again (very quickly) and do two code lines again -> which is not supposed to happen. The presence of the second "if" statement stops that.

After we remove the MouseMove event we can either proceed by moving away from the "cancel" button or by clicking on it. Both of those choices must return the MouseMove event back. That's why we have to go back to the "Button2_Click" sub and this at the bottom:
AddHandler Button2.MouseMove, AddressOf Button2_Mouse

The timer will be still fighting against our choice to transfer the focus on the "cancel" button at the moment. We need to go to the timer sub and change this line:
If Button2.Visible = True Then
into:
If Button2.Visible = True And Button2.Focused = False Then

-> so we're saying to the timer: only focus back on the "capture" button IF the "cancel" button is not focused.

Code:

    Public Class Form1

    Private x1 As Integer
    Private gg As String()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If Button2.Visible = True Then
    x1 = x1 + 1
    Label1.Text = ""
    Button2.Visible = False
    Label1.Focus()
    Exit Sub
    End If
    If CheckBox1.Checked Or CheckBox2.Checked Or CheckBox3.Checked Then
    Label1.Text = "! Scroll with your mouse and then press Space !"
    Button2.Visible = True
    End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    If CheckBox1.Checked Then
    Timer1.Enabled = True
    Else
    Timer1.Enabled = False
    End If
    If CheckBox1.Checked = False Then
    gg(x1) = ""
    TextBox1.Lines = gg
    End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If CheckBox1.Checked Then
    ReDim Preserve gg(0 To x1)
    gg(x1) = Cursor.Position.ToString
    TextBox1.Lines = gg
    End If
    If Button2.Visible = True And Button2.Focused = False Then
    Button1.Focus()
    End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Button2.Visible = False
    Label1.Text = ""
    AddHandler Button2.MouseMove, AddressOf Button2_Mouse
    End Sub
   
    Private Sub Button2_Mouse(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.MouseMove, Button2.MouseLeave
    If Button2.Focused = True Then
    Button1.Focus()
    Label1.Text = "! Scroll with your mouse and then press Space !"
    AddHandler Button2.MouseMove, AddressOf Button2_Mouse
    Exit Sub
    End If

    If Button2.Visible = True Then
    Button2.Focus()
    Label1.Text = "Cancel button selected"
    RemoveHandler Button2.MouseMove, AddressOf Button2_Mouse
    End If
    End Sub


    End Class


The program should work for the first checkbox. Adding the other two ones is quite simple. Each checkbox will have it's two variables just like the first option.

The most important part to notice is how we get the values for the other coordinates.
Inside the Timer1 sub we will have this line for checkbox2 - Form + Borders.
kk(x2) = ((Cursor.Position - Bounds.Location).ToString)

Bounds:Location - contains both x and y values. This is the location of the top-left pixel of the main field form on the whole computer screen. The value of "Bounds.Location" changes only when we drag the main form around our computer screen. Try to get the second textbox on (0,0) and then check the coordinates inside the first textbox -> those are gonna be the location values of the form bounds. Those are gonna be reduced from the whole screen coordinates in every moment to give you the results in checkbox2.

Third option (inside timer):
nn(x3) = PointToClient(Cursor.Position).ToString

-> this gives you coordinates relative to the main field form while neglecting the borders of the form.

Final code:

    Public Class Form1

    Private x1 As Integer
    Private x2 As Integer
    Private x3 As Integer

    Private gg As String()
    Private kk As String()
    Private nn As String()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If Button2.Visible = True Then
    If CheckBox1.Checked Then
    x1 = x1 + 1
    End If
    If CheckBox2.Checked Then
    x2 = x2 + 1
    End If
    If CheckBox3.Checked Then
    x3 = x3 + 1
    End If
    Label1.Text = ""
    Button2.Visible = False
    Label1.Focus()
    Exit Sub
    End If
    If CheckBox1.Checked Or CheckBox2.Checked Or CheckBox3.Checked Then
    Label1.Text = "! Scroll with your mouse and then press Space !"
    Button2.Visible = True
    End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    If CheckBox1.Checked = False Then
    gg(x1) = ""
    TextBox1.Lines = gg
    End If
    enabl()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If CheckBox1.Checked Then
    ReDim Preserve gg(0 To x1)
    gg(x1) = Cursor.Position.ToString
    TextBox1.Lines = gg
    End If
    If CheckBox2.Checked Then
    ReDim Preserve kk(0 To x2)
    kk(x2) = ((Cursor.Position - Bounds.Location).ToString)
    TextBox2.Lines = kk
    End If
    If CheckBox3.Checked Then
    ReDim Preserve nn(0 To x3)
    nn(x3) = PointToClient(Cursor.Position).ToString
    TextBox3.Lines = nn
    End If

    If Button2.Visible = True And Button2.Focused = False Then
    Button1.Focus()
    End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Button2.Visible = False
    Label1.Text = ""
    AddHandler Button2.MouseMove, AddressOf Button2_Mouse
    End Sub

    Private Sub Button2_Mouse(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.MouseMove, Button2.MouseLeave
    If Button2.Focused = True Then
    Button1.Focus()
    Label1.Text = "! Scroll with your mouse and then press Space !"
    AddHandler Button2.MouseMove, AddressOf Button2_Mouse
    Exit Sub
    End If

    If Button2.Visible = True Then
    Button2.Focus()
    Label1.Text = "Cancel button selected"
    RemoveHandler Button2.MouseMove, AddressOf Button2_Mouse
    End If
    End Sub

    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
    If CheckBox2.Checked = False Then
    kk(x2) = ""
    TextBox2.Lines = kk
    End If
    enabl()
    End Sub

    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
    If CheckBox3.Checked = False Then
    nn(x3) = ""
    TextBox3.Lines = nn
    End If
    enabl()
    End Sub

    Sub enabl()
    If CheckBox1.Checked = False And CheckBox2.Checked = False And CheckBox3.Checked = False Then
    Timer1.Enabled = False
    Else
    Timer1.Enabled = True
    End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    TextBox1.Clear()
    x1 = 0
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    TextBox2.Clear()
    x2 = 0
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    TextBox3.Clear()
    x3 = 0
    End Sub
    End Class


enabl() - this is a sub used to check if all the checkboxes are turned off, when they are (only then!) the timer is disabled. If any of the checkboxes is turned on, the timer gets turned on too.
- all three of the checkbox subs have an enabl() call

Buttons 3, 4, 5 are the "Clear" buttons below the textboxes.

If you want a quicker check of the timer, go to design page -> Timer1 -> Properties -> change interval to 10.

I hope everything makes sense. Program should work.
There probably might be ways to somewhat shorten the code too.

Visual Basic 14 - Getting Cursor Position, Timer

Create New Project.

On top left of the form add a button -> change text to: Capture coordinates
Resize button to make it a bit bigger. 
On the right of this button add another button -> change text to: Cancel, change visible to: False
On the right of the "cancel" button add a label

Below the first button add a checkbox -> change text to: Whole Screen
Below this button (middle left of the form) add a small textbox -> Mulitline: yes, size around: 140, 90
Below the textbox add a button -> text: Clear

On the right of the first textbox add two more with the same properties, so that the middle of the field form has three small textboxes. Below each one there should be one "Clear" button.
On top of the second textbox add another checkbox -> text: Form + Borders
On top of the third textbox add a checkbox -> text: Form - No Borders

Go to toolbox -> All Windows Forms -> Double click a Timer to add to the program.

The program will be able to follow three types of mouse coordinates: the whole screen of your computer, the main form of the program including borders (like the blue header) and the coordinates of the space inside the borders of the program.

We will display the coordinates inside the textboxes. The capturing of coordinates will be done with the keyboard "Space" key (any pressed key really). This allows us to get mouse coordinates outside the main program form and over various objects inside the form, which would be hard to achieve with a mouse click.

First we will focus on making one textbox operational and then, at the end, we will easily add the code for the other two.

Double click on the "Capture coordinates" button and add this inside:

If CheckBox1.Checked Or CheckBox2.Checked Or CheckBox3.Checked Then
Label1.Text = "! Scroll with your mouse and then press Space !"
Button2.Visible = True
End If

-> if any of the checkboxes is checked (checked is true), then a person can start the operations form the "capture" button - otherwise this is not possible.
Button2, in this case, is the "cancel" button on the right of the "capture" button.

We will use the timer to calculate the coordinates in every moment. When the program is started, the timer is turned off. We have to turn it on right after turning a checkbox on. First we will only do checkbox1.

Double click Checkbox1 on the design page. Add this inside:
If Checkbox1.checked Then
Timer1.Enabled = True
Else
Timer1.Enabled = False
End If

When we turn on the timer it should start sending in data to our textbox1 every 100 miliseconds (which is the default interval). We will always write this data in the last used line of the textbox and save captured coordinates in previous text lines. For doing this we will need two variables.
Right after "Public Class Form1" declare this:
Private x1 As Integer
Private gg As String()

Double click on Timer1 on the design page. Inside the sub add this:
If CheckBox1.Checked Then
ReDim Preserve gg(0 To x1)
gg(x1) = Cursor.Position.ToString
TextBox1.Lines = gg
End If

-> only if checkbox1 is checked, re-dimension "gg". The string array variable "gg" didn't have a size defined before this point. That's why we couldn't write at a specific positions into it, like:  gg(12) = "hello"
The "Preserve" modifier keeps all the content inside the array's positions. So, if we create gg(0 To 2), then write words inside gg(0), gg(1) and gg(2) - we can use preserve this words in a later re-dimensioning and give the same array a new size like gg(0 To 20).
Cursor.Position.ToString - takes the current x and y positions of the cursor on the whole screen (0,0 should be at the most top-left corner of your computer screen).

We have to be using the "gg" variable to store values inside the textbox. This is required since we can't write directly in a specific line of the textbox, for example:
TextBox1.Lines(x1) = Cursor.Position.ToString   - doesn't work. We can't choose a specific textbox line.
That's why we store all the content from the "gg" inside the textbox all the time and only change "gg".

You can check the operation of timer so far by checking the checkbox1 on.

Code:

    Public Class Form1

    Private x1 As Integer
    Private gg As String()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If CheckBox1.Checked Or CheckBox2.Checked Or CheckBox3.Checked Then
    Label1.Text = "! Scroll with your mouse and then press Space !"
    Button2.Visible = True
        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    If CheckBox1.Checked Then
    Timer1.Enabled = True
    Else
    Timer1.Enabled = False
    End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If CheckBox1.Checked Then
    ReDim Preserve gg(0 To x1)
    gg(x1) = Cursor.Position.ToString
    TextBox1.Lines = gg
    End If
    End Sub

    End Class


The first click on the "capture" button will start the process of waiting for a keyboard keypress. We will end this process by pressing the "space" button while the object selected inside the program is our "capture" button. First click starts the process, second one ends it.  

At the beginning of our button1 we will add the operation for the second (awaited) press of the button. Add:
If Button2.Visible = True Then
x1 = x1 + 1
Label1.Text = ""
Button2.Visible = False
Label1.Focus()
Exit Sub
End If

-> first button1 press makes button2 visible, therefore, this option becomes available for the second press.
x1 = x1+1  - this line is the main reason why we get a capture. Right after this line happens, the timer will write it's last cursor data in the old gg(x1) and directly start writing the new data inside the next "gg" line from now on.
After we make a capture, the label is emptied, the "cancel" button is made invisible.
At this point, we should have found a good location for our label on the form. If that's the case go to the design page, click on the label -> go to properties -> leave the "text" property empty.
Label1.Focus() - removing the focus from the "capture" is done only for esthetical reasons. Not important.

Let's add an operation to the "cancel" button. Go to the design page and double click on button2. Add:
Button2.Visible = False
Label1.Text = ""

-> pressing the "cancel" button makes it invisible and reset's it's text. This will also disable the process of capturing the cursor coordinates, since the second button press on the "capture" button is expecting a visible "cancel" button.

Currently we have a problem that must solve: after a person clicks on the "capture" button, the same button awaits for a keypress BUT the user doesn't have to do that, he can click with the left mouse button on some other object on the form (i.e. inside a textbox) and lose the focus from the "capture" button. After the focus was lost, pressing the "space" button won't press the button1 anymore. This will leave the waiting for "space" process hanging until the mouse is used on either button1 or button2.
We'll have to keep the focus on the "capture" button after it was pressed.

At the bottom of the Timer1 sub, below the "End If" line, add this:
If Button2.Visible = True Then
Button1.Focus()
End If

-> this will keep the focus on the "capture" button every 100 miliseconds. The problem of this is that this will ALSO keep the focus away from the "cancel" button (unless we click really fast). This is inconvenient. We have to be able to retain the focus on the "cancel" button when we want to click on it.
We'll add the code to fix this in the following post.

What happens when we uncheck checkbox1? Inside the checkbox1 sub , below "End If", insert:
If CheckBox1.Checked = False Then
gg(x1) = ""
TextBox1.Lines = gg
End If

-> this will empty the last line of the textbox - the 'active coordinates' line. If you removed:  gg(x1) = "" , and then checked / unchecked the checkbox, you would get like a capture inside of it you didn't want.


    Public Class Form1

    Private x1 As Integer
    Private gg As String()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If Button2.Visible = True Then
    x1 = x1 + 1
    Label1.Text = ""
    Button2.Visible = False
    Label1.Focus()
    Exit Sub
    End If
    If CheckBox1.Checked Or CheckBox2.Checked Or CheckBox3.Checked Then
    Label1.Text = "! Scroll with your mouse and then press Space !"
    Button2.Visible = True
    End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    If CheckBox1.Checked Then
    Timer1.Enabled = True
    Else
    Timer1.Enabled = False
    End If
    If CheckBox1.Checked = False Then
    gg(x1) = ""
    TextBox1.Lines = gg
    End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If CheckBox1.Checked Then
    ReDim Preserve gg(0 To x1)
    gg(x1) = Cursor.Position.ToString
    TextBox1.Lines = gg
    End If
    If Button2.Visible = True Then
    Button1.Focus()
    End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Button2.Visible = False
    Label1.Text = ""
    End Sub

    End Class


Continuing in the next post.
Go to next post.


Visual Basic 13 - Sorting Tab Pages As They Are Created, Try Catch


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


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

    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 While

While 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
 


Visual Basic 12 - Creating Tab Pages And Adding Content, Radio Buttons


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:


    Imports System.IO

    Public Class Form1

    Private f1 As String()
    Private g1 As String() = {".txt", ".pdf", ".doc"}

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    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

    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
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

-> 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.Name = "TabPage" & g
NewTab.Text = "TabPage" & g
TabControl1.TabPages.Add(NewTab)

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:
Dim textbox As New TextBox
textbox.Name = "Textbox" & g
textbox.Multiline = True
textbox.Dock = DockStyle.Fill
NewTab.Controls.Add(textbox)

-> 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:
a1 = File.OpenText(ComboBox1.SelectedItem)
NewTab.Controls("Textbox" & g).Text = a1.ReadToEnd()

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. 

NewTab.Controls("Textbox" & g).text  - if we have chosen the third radio button in our program, then the name of the name of the textbox will be "Textbox3". "Newtab" points to the new tabpages we created.
This code line looks for our specificed textbox - "go to newtabs section then find this textbox and inside it's text do this: "
a1.ReadToEnd() - read all the content from "a1" to the end

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 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