Calculator in Visual Basic

Submitted by: 
Language: 
Visitors have accessed this post 1305 times.

In this tutorial, I would like to share you this project called Calculator in Visual Basic. This project support the basic functions of addition, subtraction, division and multiplication, as well as changing the sign of the number the "+/-" key. It's up to you if you want to support the sqrt(√), percent(%), and (1/x) functions. I have a example code below.

Sample Code

Calculator Form Script
To run the code, first we will must declare the variables. And after the variables you will create a script for every buttons to add in the form. Noticing that all the number buttons are put it inside the variable Case as your buttons need to have these to call the names.

  1. Option Strict Off
  2. Option Explicit On
  3. Imports VB = Microsoft.VisualBasic
  4. Friend Class frmCalculator
  5. Inherits System.Windows.Forms.Form
  6.  
  7. Private mdblResult As Double
  8. Private mdblSavedNumber As Double
  9. Private mstrDot As String
  10. Private mstrOp As String
  11. Private mstrDisplay As String
  12. Private mblnDecEntered As Boolean
  13. Private mblnOpPending As Boolean
  14. Private mblnNewEquals As Boolean
  15. Private mblnEqualsPressed As Boolean
  16. Private mintCurrKeyIndex As Short
  17.  
  18. Private Sub frmCalculator_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
  19.  
  20. Top = VB6.TwipsToPixelsY((VB6.PixelsToTwipsY(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height) - VB6.PixelsToTwipsY(Height)) / 2)
  21. Left = VB6.TwipsToPixelsX((VB6.PixelsToTwipsX(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width) - VB6.PixelsToTwipsX(Width)) / 2)
  22.  
  23. End Sub
  24.  
  25.  
  26. Private Sub frmCalculator_KeyDown(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
  27. Dim KeyCode As Short = eventArgs.KeyCode
  28. Dim Shift As Short = eventArgs.KeyData \ &H10000
  29.  
  30. Dim intIndex As Short
  31.  
  32. Select Case KeyCode
  33. Case System.Windows.Forms.Keys.Back : intIndex = 0
  34. Case System.Windows.Forms.Keys.Delete : intIndex = 1
  35. Case System.Windows.Forms.Keys.Escape : intIndex = 2
  36. Case System.Windows.Forms.Keys.D0, System.Windows.Forms.Keys.NumPad0 : intIndex = 18
  37. Case System.Windows.Forms.Keys.D1, System.Windows.Forms.Keys.NumPad1 : intIndex = 13
  38. Case System.Windows.Forms.Keys.D2, System.Windows.Forms.Keys.NumPad2 : intIndex = 14
  39. Case System.Windows.Forms.Keys.D3, System.Windows.Forms.Keys.NumPad3 : intIndex = 15
  40. Case System.Windows.Forms.Keys.D4, System.Windows.Forms.Keys.NumPad4 : intIndex = 8
  41. Case System.Windows.Forms.Keys.D5, System.Windows.Forms.Keys.NumPad5 : intIndex = 9
  42. Case System.Windows.Forms.Keys.D6, System.Windows.Forms.Keys.NumPad6 : intIndex = 10
  43. Case System.Windows.Forms.Keys.D7, System.Windows.Forms.Keys.NumPad7 : intIndex = 3
  44. Case System.Windows.Forms.Keys.D8, System.Windows.Forms.Keys.NumPad8 : intIndex = 4
  45. Case System.Windows.Forms.Keys.D9, System.Windows.Forms.Keys.NumPad9 : intIndex = 5
  46. Case System.Windows.Forms.Keys.Decimal : intIndex = 20
  47. Case System.Windows.Forms.Keys.Add : intIndex = 21
  48. Case System.Windows.Forms.Keys.Subtract : intIndex = 16
  49. Case System.Windows.Forms.Keys.Multiply : intIndex = 11
  50. Case System.Windows.Forms.Keys.Divide : intIndex = 6
  51. Case Else : Exit Sub
  52. End Select
  53.  
  54. cmdCalc(intIndex).Focus()
  55. cmdCalc_Click(cmdCalc.Item(intIndex), New System.EventArgs())
  56.  
  57. End Sub
  58.  
  59.  
  60. Private Sub frmCalculator_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
  61. Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
  62.  
  63. Dim intIndex As Short
  64.  
  65. Select Case Chr(KeyAscii)
  66. Case "S", "s" : intIndex = 7
  67. Case "P", "p" : intIndex = 12
  68. Case "R", "r" : intIndex = 17
  69. Case "X", "x" : intIndex = 11
  70. Case "=" : intIndex = 22
  71. Case Else : GoTo EventExitSub
  72. End Select
  73.  
  74. cmdCalc(intIndex).Focus()
  75. cmdCalc_Click(cmdCalc.Item(intIndex), New System.EventArgs())
  76.  
  77. EventExitSub:
  78. eventArgs.KeyChar = Chr(KeyAscii)
  79. If KeyAscii = 0 Then
  80. eventArgs.Handled = True
  81. End If
  82. End Sub
  83.  
  84. Private Sub cmdCalc_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCalc.Click
  85. Dim Index As Short = cmdCalc.GetIndex(eventSender)
  86.  
  87. Dim strPressedKey As String
  88.  
  89. mintCurrKeyIndex = Index
  90.  
  91. If mstrDisplay = "ERROR" Then
  92. mstrDisplay = ""
  93. End If
  94.  
  95. strPressedKey = cmdCalc(Index).Text
  96.  
  97. Select Case strPressedKey
  98. Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
  99. If mblnOpPending Then
  100. mstrDisplay = ""
  101. mblnOpPending = False
  102. End If
  103. If mblnEqualsPressed Then
  104. mstrDisplay = ""
  105. mblnEqualsPressed = False
  106. End If
  107. mstrDisplay = mstrDisplay & strPressedKey
  108. Case "."
  109. If mblnOpPending Then
  110. mstrDisplay = ""
  111. mblnOpPending = False
  112. End If
  113. If mblnEqualsPressed Then
  114. mstrDisplay = ""
  115. mblnEqualsPressed = False
  116. End If
  117. If InStr(mstrDisplay, ".") > 0 Then
  118. Beep()
  119. Else
  120. mstrDisplay = mstrDisplay & strPressedKey
  121. End If
  122. Case "+", "-", "X", "/"
  123. mdblResult = Val(mstrDisplay)
  124. mstrOp = strPressedKey
  125. mblnOpPending = True
  126. mblnDecEntered = False
  127. mblnNewEquals = True
  128. Case "%"
  129. mdblSavedNumber = (Val(mstrDisplay) / 100) * mdblResult
  130. mstrDisplay = VB6.Format(mdblSavedNumber)
  131. Case "="
  132. If mblnNewEquals Then
  133. mdblSavedNumber = Val(mstrDisplay)
  134. mblnNewEquals = False
  135. End If
  136. Select Case mstrOp
  137. Case "+"
  138. mdblResult = mdblResult + mdblSavedNumber
  139. Case "-"
  140. mdblResult = mdblResult - mdblSavedNumber
  141. Case "X"
  142. mdblResult = mdblResult * mdblSavedNumber
  143. Case "/"
  144. If mdblSavedNumber = 0 Then
  145. mstrDisplay = "ERROR"
  146. Else
  147. mdblResult = mdblResult / mdblSavedNumber
  148. End If
  149. Case Else
  150. mdblResult = Val(mstrDisplay)
  151. End Select
  152. If mstrDisplay <> "ERROR" Then
  153. mstrDisplay = VB6.Format(mdblResult)
  154. End If
  155. mblnEqualsPressed = True
  156. Case "+/-"
  157. If mstrDisplay <> "" Then
  158. If VB.Left(mstrDisplay, 1) = "-" Then
  159. mstrDisplay = VB.Right(mstrDisplay, 2)
  160. Else
  161. mstrDisplay = "-" & mstrDisplay
  162. End If
  163. End If
  164. Case "Backspace"
  165. If Val(mstrDisplay) <> 0 Then
  166. mstrDisplay = VB.Left(mstrDisplay, Len(mstrDisplay) - 1)
  167. mdblResult = Val(mstrDisplay)
  168. End If
  169. Case "CE"
  170. mstrDisplay = ""
  171. Case "C"
  172. mstrDisplay = ""
  173. mdblResult = 0
  174. mdblSavedNumber = 0
  175. Case "1/x"
  176. If Val(mstrDisplay) = 0 Then
  177. mstrDisplay = "ERROR"
  178. Else
  179. mdblResult = Val(mstrDisplay)
  180. mdblResult = 1 / mdblResult
  181. mstrDisplay = VB6.Format(mdblResult)
  182. End If
  183. Case "sqrt"
  184. If Val(mstrDisplay) < 0 Then
  185. mstrDisplay = "ERROR"
  186. Else
  187. mdblResult = Val(mstrDisplay)
  188. mdblResult = System.Math.Sqrt(mdblResult)
  189. mstrDisplay = VB6.Format(mdblResult)
  190. End If
  191. End Select
  192.  
  193. If mstrDisplay = "" Then
  194. lblDisplay.Text = "0."
  195. Else
  196. mstrDot = IIf(InStr(mstrDisplay, ".") > 0, "", ".")
  197. lblDisplay.Text = mstrDisplay & mstrDot
  198. If VB.Left(lblDisplay.Text, 1) = "0" Then
  199. lblDisplay.Text = Mid(lblDisplay.Text, 2)
  200. End If
  201. End If
  202.  
  203. If lblDisplay.Text = "." Then lblDisplay.Text = "0."
  204.  
  205. End Sub
  206. End Class

And if you already done or complete your calculator project all you have to do is test it so you can know if their's an error. If you do have any problems or suggestions in your projects or programming then just visit this website www.sourcecodester.com for more updates and programming ideas.

Hope that you learn in this project and enjoy coding. Don't forget to LIKE & SHARE this website.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.