Visual Basic Tutorial

Visual Basic TutorialI recently had a large project using Visual Basic and since you guys asked for it, I decided to make a Visual Basic tutorial. In this video I’ll cover GUI Design, Event Handling, Data Types, Exception Handling, Casting, the MessageBox and more.

Visual Basic is great because it makes it very easy to create complex GUI apps that interface easily with databases and much more. All of the code used follows the video below.

If you like videos like this consider donating a $1 on Patreon

[googleplusone]

Code from the Video

lblName: Text: Name, Size: 35x13
txtName: Size: 208x20
btnOK: Text: &OK, Size 75x23
btnMessage: Text: &Message, Size: 75x23
btnExit: Text: E&xit, Size: 75x23

txtAddVal1: Size 55x20
btnAddValues: Text: +, Size: 23x23
txtAddVal2: Size: 55x20
lblAddEqual: Text: =, Size: 13x13
txtSumAnswer: Size: 55x20

txtDivideVal1: Size 55x20
btnDivideValues: Text: +, Size: 23x23
txtDivideVal2: Size: 55x20
lblDivideEqual: Text: =, Size: 13x13
txtDivisionAnswer: Size: 55x20
' This code defines what happens with your form
' We define how to handle events like a mouse click on the button
Public Class Form1

    ' This is a subroutine that doesn't return a value
    ' This code runs every time the button is clicked
    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click

        ' & is used to join or concatenate strings
        ' txtName.Text returns the value stored in the Text Box
        ' You can also assign values using txtName.Text

        txtName.Text = "Hello " & txtName.Text
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click

        ' Will close the program
        Me.Close()

    End Sub

    Private Sub btnMessage_Click(sender As Object, e As EventArgs) Handles btnMessage.Click

        ' Opens a message box and says hello to the user
        ' The text after the , is shown in the message box title bar
        MessageBox.Show("Hello " & txtName.Text,
                        "Hello " & txtName.Text)

    End Sub

    Private Sub btnAddValues_Click(sender As Object, e As EventArgs) Handles btnAddValues.Click

        ' If an error could occur surround the error causing code
        ' with a Try block
        Try

            ' Create an Integer variable and store the value
            ' Integers can hold values -2147483648 through +2147483647
            ' A Long holds -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807 
            ' Decimal holds +/-79,228,162,514,264,337,593,543,950,335
            ' and is precise to 29 decimal places
            ' Doubles hold +/-1.79769313486231570E+308 but are not precise
            ' Booleans are True or False
            ' Use CInt() to convert a String into an Integer
            Dim firstNum As Integer = CInt(txtAddVal1.Text)
            Dim secondNum As Integer = CInt(txtAddVal2.Text)

            ' Sum the values
            Dim sum = firstNum + secondNum

            ' Convert the Integer into a String
            ' You could also use sum.ToString
            txtSumAnswer.Text = CStr(sum)

            ' Catch the likely error to avoid crashing your program
        Catch Exc As System.InvalidCastException
            MessageBox.Show("Please enter numbers to sum",
                        "Error")

            ' Print to the Output console
            Console.WriteLine("An error occurred")

        Catch Exc As Exception
            MessageBox.Show("An unknown error occurred",
                        "Error")

        End Try

        ' Other common cast options
        ' CBool : Convert to a Boolean
        ' CDbl : Convert to Double
        ' CDec : Convert to Decimal
        ' CLng : Convert to Long


    End Sub

    Private Sub btnDivideValues_Click(sender As Object, e As EventArgs) Handles btnDivideValues.Click

        ' Create Decimals and convert from String
        Dim firstNum As Decimal = CDec(txtDivideVal1.Text)
        Dim secondNum As Decimal = CDec(txtDivideVal2.Text)

        ' Divide Decimals and then convert the answer into an Integer
        Dim intSolution As Integer = CType(firstNum / secondNum, Integer)

        txtDivisionAnswer.Text = CStr(intSolution)

    End Sub
End Class

Leave a Reply

Your email address will not be published. Required fields are marked *