How to create a Simple Calculator using Visual Basic 6.0 Creating a new project 1. Create a new folder for your calculator. To house all of your necessary files, do the following: Go to the location in which you want to save your VB6 Calculator. Right click a blank space. Select New in the drop-down menu Click Folder Type in calculator and press enter. A VB.Net Calculator Program Before VB.Net arrived in 2002, the highly popular Visual Basic 6 provided programmers with the ability to create what were known as control arrays.In real terms, a control array was actually a collection of controls that could be programmed as an array thanks to some fairly complex behind-the-scenes code provided by Microsoft.
The calculator above was created after I did some work crunching out the code and searching the Internet for solutions where I got stuck. Thanks to the generosity of the programming fraternity, I managed to come up with something to the best of my amateur ability. I still got stuck with one portion of the code, which I will indicate later.
Following is the full code for this program, and I call it: QuickCalc.
_____________________________________________________________________________________________
First and foremost, arrange all the necessary command buttons and a textbox onto a Form. For the textbox, I set it to read-only, and text to '0' (zero, not the letter O), named it txtInput.
Next, some essential global declarations.
1. | ‘Variables to hold operands. |
2. | Private dblValue1 As Double |
3. | Private dblValue2 As Double |
4. | ‘Variable to hold temporary values. |
5. | Private dblTemp As Double |
6. | ‘True if '.' is used, otherwise false. |
7. | Private blnDecimalPoint As Boolean |
8. | Private blnInputStatus As Boolean |
9. | ‘Variable to hold Operator. |
10. | Private strCalculate As String |
Calculator In Visual Studio
Gta vice city underground pc full version game free download. Next, the code for Number buttons 1 – 9.
1. | If (blnInputStatus AndAlso txtInput.Text <> '0') Or blnDecimalPoint Then |
2. | txtInput.Text += cmd1.Text |
3. | Else |
4. | txtInput.Text = cmd1.Text |
5. | blnInputStatus = True |
6. | End If |
The above code is for button number 1. For 2 to 9, simply change the cmd number to the respective button number.
Next, the code for Number button 0.
1. | If blnInputStatus Then |
2. | ‘To prevent multiple zeroes from appearing if 0 is the first digit. |
3. | If txtInput.Text.Length >= 1 AndAlso txtInput.Text <> '0'Then |
4. | txtInput.Text += cmd0.Text |
5. | ‘This is to allow 0 to be added after the decimal point. |
6. | ElseIf blnDecimalPoint Then |
7. | txtInput.Text += cmd0.Text |
8. | End If |
9. | End If |
Next, the code for Decimal button.
1. | ‘To make sure that no decimal point has already been input. |
2. | If Not blnDecimalPoint Then |
3. | ‘For values > 0 but < 1. |
4. | If txtInput.Text.Length < 1 Then |
5. | txtInput.Text = '0.' |
6. | Else |
7. | txtInput.Text += '.' |
8. | End If |
9. | |
10. | blnInputStatus = True |
11. | ‘To ensure only one decimal point per calculation. |
12. | blnDecimalPoint = True |
13. | End If |
Next, the code for the Addition operator.
1. | If txtInput.Text.Length <> 0 Then |
2. | ‘Check the value of the function flag. |
3. | If strCalculate = String.Empty Then |
4. | ‘Assign the value in the input box to the holder. |
5. | dblValue1 = CType(txtInput.Text, Double) |
6. | txtInput.Text = '0' |
7. | Else |
8. | ‘Call the CalculateTotals method. |
9. | CalculateTotals() |
10. | End If |
11. | |
12. | ‘Assign a value to the function flag. |
13. | strCalculate = 'Add' |
14. | |
15. | ‘Toggle the decimal flag. |
16. | blnDecimalPoint = False |
17. | End If |
The code for the Subtraction, Multiplication, and Division operators are almost identical, except that you need to change the value assigned to the function flag – Subtract, Multiply, Divide – respectively.
Next, the code for Equal button.
1. | If txtInput.Text.Length <> 0 Then |
2. | CalculateTotals() |
3. | strCalculate = String.Empty |
4. | blnDecimalPoint = False |
5. | End If |
Next, the code for the Exponential button.
1. | If txtInput.Text.Length <> 0 Then |
2. | If strCalculate = String.Empty Then |
3. | dblValue1 = CType(txtInput.Text, Double) |
4. | txtInput.Text = '0' |
5. | Else |
6. | CalculateTotals() |
7. | End If |
8. | strCalculate = 'Exponential' |
9. | blnDecimalPoint = False |
10. | End If |
Next, the code for the Square Root button.
1. | If txtInput.Text < '0'Then |
2. | MsgBox('The root of a negative number is undefined!', 0, 'WARNING') |
3. | Exit Sub |
4. | End If |
5. | |
6. | If txtInput.Text.Length <> 0 Then |
7. | dblTemp = CType(txtInput.Text, Double) |
8. | dblTemp = System.Math.Sqrt(dblTemp) |
9. | txtInput.Text = CType(dblTemp, String) |
10. | blnDecimalPoint = False |
11. | End If |
Next, the code for the Reciprocal button.
1. | If txtInput.Text.Length <> 0 Then |
2. | dblTemp = CType(txtInput.Text, Double) |
3. | dblTemp = 1 / dblTemp |
4. | txtInput.Text = CType(dblTemp, String) |
5. | blnDecimalPoint = False |
6. | End If |
Next, the code for the Positive/Negative button.
Visual Basic Code For Simple Calculator Program
1. | If txtInput.Text.Length <> 0 Then |
2. | dblTemp = CType(txtInput.Text, Double) |
3. | dblTemp *= -1 |
4. | txtInput.Text = CType(dblTemp, String) |
5. | End If |
Next, the code for the Clear All (C) button.
1. | txtInput.Text = '0' |
2. | dblValue1 = 0 |
3. | dblValue2 = 0 |
4. | strCalculate = String.Empty |
5. | blnDecimalPoint = False |
Next, the code for the Clear Entry (CE) button.
1. | txtInput.Text = '0' |
2. | blnDecimalPoint = False |
Unlock password cell phone. Next, the code for the Backspace button.
1. | ‘Declare locals needed. |
2. | Dim str As String |
3. | Dim intNumbers As Integer = txtInput.Text.Length |
4. | |
5. | If txtInput.Text.Length = 1 Or (txtInput.Text.Length = 2 AndAlso |
6. | txtInput.Text.StartsWith('-')) Then |
7. | txtInput.Text = '0' |
8. | Exit Sub |
9. | End If |
10. | |
11. | If txtInput.Text.Length > 0 Then |
12. | ‘Get the next to last character. |
13. | str = txtInput.Text.Chars(txtInput.Text.Length – 2) |
14. | ‘Check if it's a decimal. |
15. | If str = '.'Then |
16. | ‘If it is, then toggle the blnDecimalPoint flag. |
17. | blnDecimalPoint = False |
18. | txtInput.Text = txtInput.Text.Remove(intNumbers – 2) |
19. | Exit Sub |
20. | End If |
21. | txtInput.Text = txtInput.Text.Remove(intNumbers – 1) |
22. | End If |
Finally, the CalculateTotals procedure.
1. | dblValue2 = CType(txtInput.Text, Double) |
2. | Select Case strCalculate |
3. | Case'Add' |
4. | dblValue1 += dblValue2 |
5. | Case'Subtract' |
6. | dblValue1 -= dblValue2 |
7. | Case'Multiply' |
8. | dblValue1 *= dblValue2 |
9. | Case'Divide' |
10. | If dblValue2 = 0 Then |
11. | MsgBox('You cannot divide by 0!', 0, 'WARNING') |
12. | Exit Sub |
13. | End If |
14. | dblValue1 /= dblValue2 |
15. | Case'Exponential' |
16. | dblValue1 ^= dblValue2 |
17. | End Select |
18. | txtInput.Text = CType(dblValue1, String) |
19. | blnInputStatus = False |
_____________________________________________________________________________________________
Above is the code for this calculator program.
!PROBLEM!
However, there is an area which I have some niggling issues with.
1. For the + – * / and ^ operators, if I were to click on one of them multiple times after I input the first value, they will go performing the operation on their own without me entering the second value. How do I stop this from happening?
How to write a visual basic program. Visual Basic programs for beginners with examples. How to print a string in visual basic. below are some examples of visual basic programs.
In this tutorial, Will see some basic string operation like how to print string and char in visual basic. Check thevisual basic program for mathematical operations.
Let's start with the basic 'Hello World' Project in Visual basic. Start any programming language with some string operation is a really good idea.
Calculator In Visual Studio
Gta vice city underground pc full version game free download. Next, the code for Number buttons 1 – 9.
1. | If (blnInputStatus AndAlso txtInput.Text <> '0') Or blnDecimalPoint Then |
2. | txtInput.Text += cmd1.Text |
3. | Else |
4. | txtInput.Text = cmd1.Text |
5. | blnInputStatus = True |
6. | End If |
The above code is for button number 1. For 2 to 9, simply change the cmd number to the respective button number.
Next, the code for Number button 0.
1. | If blnInputStatus Then |
2. | ‘To prevent multiple zeroes from appearing if 0 is the first digit. |
3. | If txtInput.Text.Length >= 1 AndAlso txtInput.Text <> '0'Then |
4. | txtInput.Text += cmd0.Text |
5. | ‘This is to allow 0 to be added after the decimal point. |
6. | ElseIf blnDecimalPoint Then |
7. | txtInput.Text += cmd0.Text |
8. | End If |
9. | End If |
Next, the code for Decimal button.
1. | ‘To make sure that no decimal point has already been input. |
2. | If Not blnDecimalPoint Then |
3. | ‘For values > 0 but < 1. |
4. | If txtInput.Text.Length < 1 Then |
5. | txtInput.Text = '0.' |
6. | Else |
7. | txtInput.Text += '.' |
8. | End If |
9. | |
10. | blnInputStatus = True |
11. | ‘To ensure only one decimal point per calculation. |
12. | blnDecimalPoint = True |
13. | End If |
Next, the code for the Addition operator.
1. | If txtInput.Text.Length <> 0 Then |
2. | ‘Check the value of the function flag. |
3. | If strCalculate = String.Empty Then |
4. | ‘Assign the value in the input box to the holder. |
5. | dblValue1 = CType(txtInput.Text, Double) |
6. | txtInput.Text = '0' |
7. | Else |
8. | ‘Call the CalculateTotals method. |
9. | CalculateTotals() |
10. | End If |
11. | |
12. | ‘Assign a value to the function flag. |
13. | strCalculate = 'Add' |
14. | |
15. | ‘Toggle the decimal flag. |
16. | blnDecimalPoint = False |
17. | End If |
The code for the Subtraction, Multiplication, and Division operators are almost identical, except that you need to change the value assigned to the function flag – Subtract, Multiply, Divide – respectively.
Next, the code for Equal button.
1. | If txtInput.Text.Length <> 0 Then |
2. | CalculateTotals() |
3. | strCalculate = String.Empty |
4. | blnDecimalPoint = False |
5. | End If |
Next, the code for the Exponential button.
1. | If txtInput.Text.Length <> 0 Then |
2. | If strCalculate = String.Empty Then |
3. | dblValue1 = CType(txtInput.Text, Double) |
4. | txtInput.Text = '0' |
5. | Else |
6. | CalculateTotals() |
7. | End If |
8. | strCalculate = 'Exponential' |
9. | blnDecimalPoint = False |
10. | End If |
Next, the code for the Square Root button.
1. | If txtInput.Text < '0'Then |
2. | MsgBox('The root of a negative number is undefined!', 0, 'WARNING') |
3. | Exit Sub |
4. | End If |
5. | |
6. | If txtInput.Text.Length <> 0 Then |
7. | dblTemp = CType(txtInput.Text, Double) |
8. | dblTemp = System.Math.Sqrt(dblTemp) |
9. | txtInput.Text = CType(dblTemp, String) |
10. | blnDecimalPoint = False |
11. | End If |
Next, the code for the Reciprocal button.
1. | If txtInput.Text.Length <> 0 Then |
2. | dblTemp = CType(txtInput.Text, Double) |
3. | dblTemp = 1 / dblTemp |
4. | txtInput.Text = CType(dblTemp, String) |
5. | blnDecimalPoint = False |
6. | End If |
Next, the code for the Positive/Negative button.
Visual Basic Code For Simple Calculator Program
1. | If txtInput.Text.Length <> 0 Then |
2. | dblTemp = CType(txtInput.Text, Double) |
3. | dblTemp *= -1 |
4. | txtInput.Text = CType(dblTemp, String) |
5. | End If |
Next, the code for the Clear All (C) button.
1. | txtInput.Text = '0' |
2. | dblValue1 = 0 |
3. | dblValue2 = 0 |
4. | strCalculate = String.Empty |
5. | blnDecimalPoint = False |
Next, the code for the Clear Entry (CE) button.
1. | txtInput.Text = '0' |
2. | blnDecimalPoint = False |
Unlock password cell phone. Next, the code for the Backspace button.
1. | ‘Declare locals needed. |
2. | Dim str As String |
3. | Dim intNumbers As Integer = txtInput.Text.Length |
4. | |
5. | If txtInput.Text.Length = 1 Or (txtInput.Text.Length = 2 AndAlso |
6. | txtInput.Text.StartsWith('-')) Then |
7. | txtInput.Text = '0' |
8. | Exit Sub |
9. | End If |
10. | |
11. | If txtInput.Text.Length > 0 Then |
12. | ‘Get the next to last character. |
13. | str = txtInput.Text.Chars(txtInput.Text.Length – 2) |
14. | ‘Check if it's a decimal. |
15. | If str = '.'Then |
16. | ‘If it is, then toggle the blnDecimalPoint flag. |
17. | blnDecimalPoint = False |
18. | txtInput.Text = txtInput.Text.Remove(intNumbers – 2) |
19. | Exit Sub |
20. | End If |
21. | txtInput.Text = txtInput.Text.Remove(intNumbers – 1) |
22. | End If |
Finally, the CalculateTotals procedure.
1. | dblValue2 = CType(txtInput.Text, Double) |
2. | Select Case strCalculate |
3. | Case'Add' |
4. | dblValue1 += dblValue2 |
5. | Case'Subtract' |
6. | dblValue1 -= dblValue2 |
7. | Case'Multiply' |
8. | dblValue1 *= dblValue2 |
9. | Case'Divide' |
10. | If dblValue2 = 0 Then |
11. | MsgBox('You cannot divide by 0!', 0, 'WARNING') |
12. | Exit Sub |
13. | End If |
14. | dblValue1 /= dblValue2 |
15. | Case'Exponential' |
16. | dblValue1 ^= dblValue2 |
17. | End Select |
18. | txtInput.Text = CType(dblValue1, String) |
19. | blnInputStatus = False |
_____________________________________________________________________________________________
Above is the code for this calculator program.
!PROBLEM!
However, there is an area which I have some niggling issues with.
1. For the + – * / and ^ operators, if I were to click on one of them multiple times after I input the first value, they will go performing the operation on their own without me entering the second value. How do I stop this from happening?
How to write a visual basic program. Visual Basic programs for beginners with examples. How to print a string in visual basic. below are some examples of visual basic programs.
In this tutorial, Will see some basic string operation like how to print string and char in visual basic. Check thevisual basic program for mathematical operations.
Let's start with the basic 'Hello World' Project in Visual basic. Start any programming language with some string operation is a really good idea.
Write a visual basic program to print a string 'Hello World'
The below code will print the string value 'Hello World'. Console.WriteLine(' ') is used to print any value as an output and the Console.ReadLine() is used to read the next line here we are using it to hold the screen.
Output: Hello World
Visual basic program to print a string variable.
Declare a variable in visual basic is really simple. here in the below code. Dim str As String is a variable decoration. Where str is a variable of string type.
Output: Write First Program in Visual basic
How to Concat two string in Visual basic.
+ or the & operator is used to Concat two or more string in Visual basic. Below is the code to Concat two string in visual basic. Which contains 3 strings str1, str2,str3.
Output: Visual basic program
Please check more examples on visual basic program for beginner
Visual Basic programs with example
Basic Vb programs
Example 2.1.1 |
Example 2.1.2 |
You can also use the + or the & operator to join two or more texts (string) together like in example 2.1.4 (a) and (b)
Example 2.1.4(a) Private Sub A = 'Tom' End Sub | Example 2.1.4(b) Private Sub A = 'Tom' End Sub |
Write a VB program to convert Celsius to Fahrenheit