Visual Basic Calculator Code using Control Arrays

This article is written by Pon Saravanan  on 09-Sep-09 Last modified on :19-Jan-10

Subscribe to my RSS Feed!

VB calculator code

This article is mainly for the beginners. I am trying to explain the use of Control Arrays with the help of Calculator Sample.I feel control arrays are overlooked  in classic visual basic. if the way to implement is understood, it is much useful in reducing the code. Control Arrays can be used in the situations like whenever the need of same controls being used several times in an application. We can make them as an array, and the controls can be accessed by their index in all the respective events

VB 6 Control Arrays in form

As shown in the following figure, Design the form. Create a Command Button named as Command1 and copy that and paste it 11 times. This will create a control array.
Then assign the text as shown in the figure. all the controls under red boxes are under one group. So design it accordingly.

like wise create cmdOperator control array for the green box group.

Now you can use the sample code to try


VB Calculator

we actually storing the Last Operator used and The first part of the number used in a class level variable. so that they can be accessed across the events and functions. Since the operators also created as control arrays. we had drastically reduced the amount of code required for a simple calculator.

Source Code - Simple(VB 6.0)

Private LastOperator  As String
Private FirstPart As String

Private Sub cmdClear_Click()
    FirstPart = ""
    LastOperator = ""
End Sub
Private Sub cmdEquals_Click()
Select Case LastOperator
    Case Is = "+"
        Text1.Text = FirstPart + Text1.Text
    Case Is = "-"
        Text1.Text = FirstPart - Text1.Text
    Case Is = "/"
        Text1.Text = FirstPart / Text1.Text
    Case Is = "*"
        Text1.Text = FirstPart * Text1.Text
End Select
    FirstPart = ""
    LastOperator = ""
End Sub
Private Sub cmdOperator_Click(Index As Integer)
    LastOperator = cmdOperator(Index).Caption
    FirstPart = Text1.Text
    Text1.Text = ""
End Sub
Private Sub Command1_Click(Index As Integer)
    Text1.Text = Text1.Text + Command1(Index).Caption
End Sub


Source Code - Enhanced Calculator(VB 6.0)

Now few places we need to lock the Calculator stop entering invalid entries. If not done then runtime error will be thrown. To prevent entering the alpha characters the textbox can be locked by enabled=false. 

For improved usability, we can hook the keyboard to handle most of the keystrokes to minimize the use of mouse. After enabling KeyPreview in the Form, all the key strokes will be routed through the form. So handling event of form’s keypress will be able to handle the keyboard keys.

Source Code - Enhanced Calculator(VB 6.0)

Private LastOperator  As String
Private FirstPart As String
Private Sub Form_Load()
    txtResult.Enabled = False
    Me.KeyPreview = True
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    ' numbers in main keyboard
    If (KeyCode >= 48 And KeyCode <= 57) Then
        AddDigits (Chr(KeyCode))
    End If
   
    ' numbers in numpad
    If (KeyCode >= 96 And KeyCode <= 105) Then
        AddDigits (Chr(KeyCode - 48))
    End If
   
    'if backspace
    If (KeyCode = 8) Then
        If (txtResult.Text <> "") Then
        txtResult.Text = Left(txtResult.Text, Len(txtResult.Text) - 1)
        End If
    End If
    'for various operators
    If (KeyCode = 111) Then RegisterOperator ("/")
    If (KeyCode = 106) Then RegisterOperator ("*")
    If (KeyCode = 109) Then RegisterOperator ("-")
    If (KeyCode = 107) Then RegisterOperator ("+")
End Sub
Private Sub cmdOperator_Click(Index As Integer)
    RegisterOperator (cmdOperator(Index).Caption)
End Sub
Private Sub cmdClear_Click()
    FirstPart = ""
    LastOperator = ""
    txtResult.Text = ""
   
End Sub
Private Sub cmdEquals_Click()
    Select Case LastOperator
        Case Is = "+"
            txtResult.Text = FirstPart + txtResult.Text
        Case Is = "-"
            txtResult.Text = FirstPart - txtResult.Text
        Case Is = "/"
            txtResult.Text = FirstPart / txtResult.Text
        Case Is = "*"
            txtResult.Text = FirstPart * txtResult.Text
    End Select
        FirstPart = ""
        LastOperator = ""
End Sub
Private Sub Command1_Click(Index As Integer)
    AddDigits (Command1(Index).Caption)
End Sub
 
Private Sub AddDigits(Digit As String)
    txtResult.Text = txtResult.Text & Digit
End Sub
Private Sub RegisterOperator(OperatorText As String)
    LastOperator = OperatorText
    FirstPart = txtResult.Text
    txtResult.Text = ""
End Sub









GUEST
hi thanks 9/16/2009 7:28:10 AM

GUEST
wow nice tnx i can use this 10/19/2009 1:00:37 AM

GUEST
i really need to know how to use or make a code for my project..we're
planning to do some program which is cybercafe program but we really dont know how to use coding..we're done with our interface..the problem is the coding..
10/27/2009 7:23:44 AM

GUEST
11/24/2009 2:48:36 AM

GUEST
..tnx a lot!! 1/24/2010 7:12:37 PM


Comments
   
Captcha Image
For you specially:  
Captcha Text Enter the text in the image.(Not Case sensitive)    




Spam Bot Trap