Saturday, April 12, 2014

Visual Basic Tutorial : How to Compute Numbers In Visual Basic [Mathematical Operations] With Textboxes

Hi all, in this tutorial we will tackle about mathematical computing in vb.net (visual basic 2010).. We all know that computers works with the same basic principles.. Computers are built decades ago to use for powerful computing.. A single characters in your keyboards are represented as binaries as zeros and one ("0 1").

Developing Simple or Advance Calculators in visual basic are made easier using the batch of codes to be written under every controls..

Before we start let's first take an overview on what are the operators to be used in computing on visual basic..

The following is a table of arithmetic operators in Visual Basic.

SymbolName
+Addition
-Subtraction
*Multiplication
/Division
\Integer Division
ModModulo
^Exponentiation

Let's make a simple calculator...

Things you need:

1 Button = Compute
1 Textbox = Num1
1 Textbox = Num2
1 Textbox = Total
4 Radiobuttons = (operators +, -, *, /)


The code Template
total = num1 + num2 for addition

total = num1 - num2 for subtraction
total = num1 * num2 for multiplication
total = num1 / num2 for division


Design it like this...
How to Compute Numbers In Visual Basic [Mathematical Operations] With Textboxes

Now for coding as I said last time we will use the [multi-line syntax IF Statement] that is why we use radioboxes for condition making... Learn Multi-Line IF Statement Syntax on this section..

Now lets insert a code for our calculator..

Double click the Button1 and use this code..

Note* from the design Do not change the name of the controls to avoid errors..

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

  
        If RadioButton1.Checked Then
            TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text)

        ElseIf RadioButton2.Checked Then
            TextBox3.Text = Val(TextBox1.Text) - Val(TextBox2.Text)

        ElseIf RadioButton3.Checked Then
            TextBox3.Text = Val(TextBox1.Text) * Val(TextBox2.Text)

        ElseIf RadioButton4.Checked Then
            TextBox3.Text = Val(TextBox1.Text) / Val(TextBox2.Text)

        End If
End Sub

Based from the code above you can see that it is very simple to analyze how it works, it is very self explanatory as you read it.. Now try it yourself and experiment use other controls for your own calculator..

Watch My Video Tutorial


Incoming search terms:

how to compute in visual basic
how to compute in visual basic using textbox1 and textbox2

No comments:

Post a Comment