First Step: Open your Microsoft Visual Studio 2008/2010 and create new project and name your project.
Second Step: In your Form1 add one(1) textbox and twenty six(26) button to your form
Form1 = FormCalculator
TextBox1 = txtscr
Button1 = btnmc - MC
Button2 = btnmr - MR
Button3 = btnms - MS
Button4 = btnmad - M+
Button5 = btnbps - <
Button6 = btnclr - C
Button7 = btnpn - ±
Button8 = btnsqu - √
Button9 = btn1 - 1
Button10 = btn2 - 2
Button11 = btn3 -3
Button12 = btn4 - 4
Button13 = btn5 - 5
Button14 = btn6 - 6
Button15 = btn7 - 7
Button16 = btn8 - 8
Button17 = btn9 - 9
Button18 =btn0 - 0
Button19 =btndot - .
Button20 = btnadd - +
Button21 = btnsub - -
Button22 = btnmul - *
Button23 = btndiv - /
Button24 = btnper - %
Button25 = btnint - ¹/แตก
Button26 = btnequ - =
Add this piece of code after Public Class FormCalculator
Dim Value1 As Double
Dim Value2 As Double
Dim tempMemory As Double
Dim tempMem As Double
Dim tempOperation As String
Dim hasDecimal As Boolean
Dim tempValue As Double
Like This:
Public Class FormCalculator
Dim Value1 As Double
Dim Value2 As Double
Dim tempMemory As Double
Dim tempMem As Double
Dim tempOperation As String
Dim hasDecimal As Boolean
Dim tempValue As Double
Then lets code Button:
(MC) Button. This button means Memory Clear
Private Sub btnmc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmc.Click
tempMemory = vbNullString
End Sub
(MR) Button. Memory Recall
Private Sub btnmr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmr.Click
If tempMemory <> "0" Then
txtscr.Text = tempMemory
End If
End Sub
(MS) Button. Memory Stores
Private Sub btnms_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnms.Click
If txtscr.Text.Length >= 1 Then
tempMemory = txtscr.Text
End If
End Sub
(M+)Button. Add to Memory
Private Sub btnmad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmad.Click
If tempMemory <> "0" Then
tempMem = tempMemory + txtscr.Text
txtscr.Text = tempMem.ToString()
tempMemory = txtscr.Text
End If
End Sub
(<)Button. BackSpace
Private Sub btnbps_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbps.Click
Dim tempSting As String
Dim tempInteger As Integer
If txtscr.Text.Length > 0 Then
tempSting = txtscr.Text.Chars(txtscr.Text.Length - 1)
If tempSting = "." Then
End If
tempInteger = txtscr.Text.Length
txtscr.Text = txtscr.Text.Remove(tempInteger - 1, 1)
End If
End Sub
(C)Button. Clear All
Private Sub btnclr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclr.Click
txtscr.Text = vbNullString
End Sub
(±)Button. Positive and Negative Symbols
Private Sub btnpn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpn.Click
txtscr.Text = -1 * txtscr.Text
End Sub
(√)Button. Square Root
Private Sub btnsqu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsqu.Click
If txtscr.Text.Length <> 0 Then
TempValue = CType(txtscr.Text, Double)
TempValue = System.Math.Sqrt(TempValue)
txtscr.Text = CType(TempValue, String)
hasDecimal = False
End If
End Sub
(1-9)Button. Number1 to 9 put this code only to Button Code
txtscr.Text = txtscr.Text & sender.Text
(0)Button. Number0
Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
If txtscr.Text.Length >= 1 Then
txtscr.Text = txtscr.Text & sender.Text
End If
End Sub
(.)Button. Decimal Point
Private Sub btndot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndot.Click
If InStr(txtscr.Text, ".") > 0 Then
Exit Sub
Else
If txtscr.Text.Length >= 1 Then
txtscr.Text = txtscr.Text & sender.text
Else
txtscr.Text = txtscr.Text & btn0.Text + sender.text
End If
End If
End Sub
(+)(-)(*)(/)Operation Button.
Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
Value1 = Val(txtscr.Text)
txtscr.Text = ""
txtscr.Focus()
tempOperation = "+"
End Sub
Private Sub btndiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndiv.Click
Value1 = Val(txtscr.Text)
txtscr.Text = ""
txtscr.Focus()
tempOperation = "/"
End Sub
Private Sub btnmul_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmul.Click
Value1 = Val(txtscr.Text)
txtscr.Text = ""
txtscr.Focus()
tempOperation = "*"
End Sub
Private Sub btnsub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsub.Click
Value1 = Val(txtscr.Text)
txtscr.Text = ""
txtscr.Focus()
tempOperation = "-"
End Sub
(%)Button.
Private Sub btnper_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnper.Click
Dim tempResult As Double
Value2 = Val(txtscr.Text)
Select Case tempOperation
Case "+"
tempResult = (Value1 + Value2) * (1 / 100)
txtscr.Text = tempResult.ToString()
Case "-"
tempResult = (Value1 - Value2) * (1 / 100)
txtscr.Text = tempResult.ToString()
Case "/"
tempResult = (Value1 / Value2) * (1 / 100)
txtscr.Text = tempResult.ToString()
Case "*"
tempResult = (Value1 * Value2) * (1 / 100)
txtscr.Text = tempResult.ToString()
End Select
txtscr.Text = tempResult.ToString()
End Sub
(¹/แตก)Button.
Private Sub btnint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnint.Click
Dim convert As Single
If txtscr.Text <> 0 Then
convert = 1 / Val(txtscr.Text)
txtscr.Text = convert
End If
End Sub
(=)Button.
Dim tempResult As Double
Value2 = Val(txtscr.Text)
Select Case tempOperation
Case "+"
tempResult = Value1 + Value2
txtscr.Text = tempResult.ToString()
Case "-"
tempResult = Value1 - Value2
txtscr.Text = tempResult.ToString()
Case "/"
tempResult = Value1 / Value2
txtscr.Text = tempResult.ToString()
Case "*"
tempResult = Value1 * Value2
txtscr.Text = tempResult.ToString()
End Select
txtscr.Text = tempResult.ToString()
End Sub
Now that should be it, you have a very simple Calculator in vb.net.
If you want the copy of this project. Here's the link.
http://adf.ly/1LwCkd
This Project Design and Program By: lyo
0 comments:
Post a Comment