Berikut ini adalah implementasi array pada aplikasi kalkulator. Komponen button dijadikan sebagai array sehingga untuk penomoran angka digunakan satu nama button dengan indeks sebagai pembedanya, begitu juga dengan button operator.
Public Class Kalkulator_Sederhana
Dim Operand1 As Double
Dim Operand2 As Double
Dim [Operator] As String
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click,
Button3.Click, Button4.Click, Button5.Click, Button6.Click,
Button7.Click, Button8.Click, Button9.Click, Button11.Click
TextBox1.Text = TextBox1.Text & sender.text
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
If InStr(TextBox1.Text, ".") > 0 Then
Exit Sub
Else
TextBox1.Text = TextBox1.Text & "."
End If
End Sub
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
TextBox1.Text = ""
End Sub
Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
Operand1 = Val(TextBox1.Text)
TextBox1.Text = ""
TextBox1.Focus()
[Operator] = "+"
End Sub
Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
Operand1 = Val(TextBox1.Text)
TextBox1.Text = ""
TextBox1.Focus()
[Operator] = "-"
End Sub
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
Operand1 = Val(TextBox1.Text)
TextBox1.Text = ""
TextBox1.Focus()
[Operator] = "*"
End Sub
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
Operand1 = Val(TextBox1.Text)
TextBox1.Text = ""
TextBox1.Focus()
[Operator] = "/"
End Sub
Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
Dim convert As Single
If TextBox1.Text <> 0 Then
convert = 1 / Val(TextBox1.Text)
TextBox1.Text = convert
End If
End Sub
Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
TextBox1.Text = -1 * TextBox1.Text
End Sub
Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click
Dim Result As Double
Operand2 = Val(TextBox1.Text)
'If [Operator] = "+" Then
' Result = Operand1 + Operand2
'ElseIf [Operator] = "-" Then
' Result = Operand1 - Operand2
'ElseIf [Operator] = "/" Then
' Result = Operand1 / Operand2
'ElseIf [Operator] = "*" Then
' Result = Operand1 * Operand2
'End If
Select Case [Operator]
Case "+"
Result = Operand1 + Operand2
MsgBox(Result.ToString("#,###.00"), MsgBoxStyle.Information, "Result")
TextBox1.Text = Result.ToString("#,###.00")
Case "-"
Result = Operand1 - Operand2
MsgBox(Result.ToString("#,###.00"), MsgBoxStyle.Information, "Result")
TextBox1.Text = Result.ToString("#,###.00")
Case "/"
Result = Operand1 / Operand2
MsgBox(Result.ToString("#,###.00"), MsgBoxStyle.Information, "Result")
TextBox1.Text = Result.ToString("#,###.00")
Case "*"
Result = Operand1 * Operand2
MsgBox(Result.ToString("#,###.00"), MsgBoxStyle.Information, "Result")
TextBox1.Text = Result.ToString("#,###.00")
End Select
TextBox1.Text = Result.ToString("#,###.00")
End Sub
0 Response to "Penggunaan Array pada Kalkulator menggunakan Visual Basic Dot Net"
Posting Komentar