itcopt
asked on
Appending two variables
Hello,
I am working on a program and need to append two variables together in a text box. The finished program will be a simple calculator. I have one text box where I will append the numbers as they are entered and calculate when a equal button is clicked.
How do I append numbers in a text box?
Thanks!
I am working on a program and need to append two variables together in a text box. The finished program will be a simple calculator. I have one text box where I will append the numbers as they are entered and calculate when a equal button is clicked.
How do I append numbers in a text box?
Thanks!
ASKER
I tried your suggestion, I came up with (an example) 1+1+1+8+4
I need to display when one of the numeric buttons on the calculator is pressed, the value is appended to the label appearing at the top of the form. The calculator should display whatever is pressed in the label, such as
3+4-1+2/2 In the label
Then when I press the equal button the sum of the numbers is displayed.
I need to display when one of the numeric buttons on the calculator is pressed, the value is appended to the label appearing at the top of the form. The calculator should display whatever is pressed in the label, such as
3+4-1+2/2 In the label
Then when I press the equal button the sum of the numbers is displayed.
Well the first part of your question is easy:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon tainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Butto n
Friend WithEvents Button2 As System.Windows.Forms.Butto n
Friend WithEvents Button3 As System.Windows.Forms.Butto n
Friend WithEvents Button4 As System.Windows.Forms.Butto n
Friend WithEvents Button5 As System.Windows.Forms.Butto n
Friend WithEvents Button6 As System.Windows.Forms.Butto n
Friend WithEvents Button7 As System.Windows.Forms.Butto n
Friend WithEvents Button8 As System.Windows.Forms.Butto n
Friend WithEvents Button9 As System.Windows.Forms.Butto n
Friend WithEvents Button17 As System.Windows.Forms.Butto n
Friend WithEvents Button18 As System.Windows.Forms.Butto n
Friend WithEvents Button11 As System.Windows.Forms.Butto n
Friend WithEvents Button12 As System.Windows.Forms.Butto n
Friend WithEvents Button13 As System.Windows.Forms.Butto n
Friend WithEvents Button14 As System.Windows.Forms.Butto n
Friend WithEvents Button10 As System.Windows.Forms.Butto n
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Button19 As System.Windows.Forms.Butto n
Friend WithEvents Button15 As System.Windows.Forms.Butto n
Friend WithEvents Button16 As System.Windows.Forms.Butto n
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Butto n
Me.Button2 = New System.Windows.Forms.Butto n
Me.Button3 = New System.Windows.Forms.Butto n
Me.Button4 = New System.Windows.Forms.Butto n
Me.Button5 = New System.Windows.Forms.Butto n
Me.Button6 = New System.Windows.Forms.Butto n
Me.Button7 = New System.Windows.Forms.Butto n
Me.Button8 = New System.Windows.Forms.Butto n
Me.Button9 = New System.Windows.Forms.Butto n
Me.Button17 = New System.Windows.Forms.Butto n
Me.Button18 = New System.Windows.Forms.Butto n
Me.Button11 = New System.Windows.Forms.Butto n
Me.Button12 = New System.Windows.Forms.Butto n
Me.Button13 = New System.Windows.Forms.Butto n
Me.Button14 = New System.Windows.Forms.Butto n
Me.Button10 = New System.Windows.Forms.Butto n
Me.Label1 = New System.Windows.Forms.Label
Me.Button19 = New System.Windows.Forms.Butto n
Me.Button15 = New System.Windows.Forms.Butto n
Me.Button16 = New System.Windows.Forms.Butto n
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 40)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(32, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "7"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(48, 40)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(32, 32)
Me.Button2.TabIndex = 1
Me.Button2.Text = "8"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(88, 40)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(32, 32)
Me.Button3.TabIndex = 2
Me.Button3.Text = "9"
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(8, 80)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(32, 32)
Me.Button4.TabIndex = 3
Me.Button4.Text = "4"
'
'Button5
'
Me.Button5.Location = New System.Drawing.Point(48, 80)
Me.Button5.Name = "Button5"
Me.Button5.Size = New System.Drawing.Size(32, 32)
Me.Button5.TabIndex = 4
Me.Button5.Text = "5"
'
'Button6
'
Me.Button6.Location = New System.Drawing.Point(88, 80)
Me.Button6.Name = "Button6"
Me.Button6.Size = New System.Drawing.Size(32, 32)
Me.Button6.TabIndex = 5
Me.Button6.Text = "6"
'
'Button7
'
Me.Button7.Location = New System.Drawing.Point(8, 120)
Me.Button7.Name = "Button7"
Me.Button7.Size = New System.Drawing.Size(32, 32)
Me.Button7.TabIndex = 6
Me.Button7.Text = "1"
'
'Button8
'
Me.Button8.Location = New System.Drawing.Point(48, 120)
Me.Button8.Name = "Button8"
Me.Button8.Size = New System.Drawing.Size(32, 32)
Me.Button8.TabIndex = 7
Me.Button8.Text = "2"
'
'Button9
'
Me.Button9.Location = New System.Drawing.Point(88, 120)
Me.Button9.Name = "Button9"
Me.Button9.Size = New System.Drawing.Size(32, 32)
Me.Button9.TabIndex = 8
Me.Button9.Text = "3"
'
'Button17
'
Me.Button17.Location = New System.Drawing.Point(8, 200)
Me.Button17.Name = "Button17"
Me.Button17.Size = New System.Drawing.Size(32, 32)
Me.Button17.TabIndex = 9
Me.Button17.Text = "C"
'
'Button18
'
Me.Button18.Location = New System.Drawing.Point(48, 200)
Me.Button18.Name = "Button18"
Me.Button18.Size = New System.Drawing.Size(32, 32)
Me.Button18.TabIndex = 10
Me.Button18.Text = "CE"
'
'Button11
'
Me.Button11.Location = New System.Drawing.Point(128, 40)
Me.Button11.Name = "Button11"
Me.Button11.Size = New System.Drawing.Size(32, 32)
Me.Button11.TabIndex = 11
Me.Button11.Text = "/"
'
'Button12
'
Me.Button12.Location = New System.Drawing.Point(128, 80)
Me.Button12.Name = "Button12"
Me.Button12.Size = New System.Drawing.Size(32, 32)
Me.Button12.TabIndex = 12
Me.Button12.Text = "X"
'
'Button13
'
Me.Button13.Location = New System.Drawing.Point(128, 120)
Me.Button13.Name = "Button13"
Me.Button13.Size = New System.Drawing.Size(32, 32)
Me.Button13.TabIndex = 13
Me.Button13.Text = "-"
'
'Button14
'
Me.Button14.Location = New System.Drawing.Point(128, 160)
Me.Button14.Name = "Button14"
Me.Button14.Size = New System.Drawing.Size(32, 32)
Me.Button14.TabIndex = 14
Me.Button14.Text = "+"
'
'Button10
'
Me.Button10.Location = New System.Drawing.Point(48, 160)
Me.Button10.Name = "Button10"
Me.Button10.Size = New System.Drawing.Size(32, 32)
Me.Button10.TabIndex = 15
Me.Button10.Text = "0"
'
'Label1
'
Me.Label1.Anchor = CType(((System.Windows.For ms.AnchorS tyles.Top Or System.Windows.Forms.Ancho rStyles.Le ft) _
Or System.Windows.Forms.Ancho rStyles.Ri ght), System.Windows.Forms.Ancho rStyles)
Me.Label1.BorderStyle = System.Windows.Forms.Borde rStyle.Fix edSingle
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(152, 24)
Me.Label1.TabIndex = 16
Me.Label1.TextAlign = System.Drawing.ContentAlig nment.Midd leRight
'
'Button19
'
Me.Button19.Location = New System.Drawing.Point(88, 200)
Me.Button19.Name = "Button19"
Me.Button19.Size = New System.Drawing.Size(72, 32)
Me.Button19.TabIndex = 19
Me.Button19.Text = "="
'
'Button15
'
Me.Button15.Location = New System.Drawing.Point(8, 160)
Me.Button15.Name = "Button15"
Me.Button15.Size = New System.Drawing.Size(32, 32)
Me.Button15.TabIndex = 20
Me.Button15.Text = "("
'
'Button16
'
Me.Button16.Location = New System.Drawing.Point(88, 160)
Me.Button16.Name = "Button16"
Me.Button16.Size = New System.Drawing.Size(32, 32)
Me.Button16.TabIndex = 21
Me.Button16.Text = ")"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(168, 238)
Me.Controls.Add(Me.Button1 6)
Me.Controls.Add(Me.Button1 5)
Me.Controls.Add(Me.Button1 9)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button1 0)
Me.Controls.Add(Me.Button1 4)
Me.Controls.Add(Me.Button1 3)
Me.Controls.Add(Me.Button1 2)
Me.Controls.Add(Me.Button1 1)
Me.Controls.Add(Me.Button1 8)
Me.Controls.Add(Me.Button1 7)
Me.Controls.Add(Me.Button9 )
Me.Controls.Add(Me.Button8 )
Me.Controls.Add(Me.Button7 )
Me.Controls.Add(Me.Button6 )
Me.Controls.Add(Me.Button5 )
Me.Controls.Add(Me.Button4 )
Me.Controls.Add(Me.Button3 )
Me.Controls.Add(Me.Button2 )
Me.Controls.Add(Me.Button1 )
Me.Name = "Form1"
Me.Text = "Calculator"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button_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, _
Button10.Click, Button11.Click, Button12.Click, Button13.Click, Button14.Click, _
Button15.Click, Button16.Click, Button17.Click, Button18.Click
Select Case sender.text
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "(", ")"
Label1.Text = Label1.Text & sender.text
Case "/", "X", "-", "+"
If Not (Label1.Text.EndsWith("/") Or Label1.Text.EndsWith("X") Or _
Label1.Text.EndsWith("-") Or Label1.Text.EndsWith("+") Or _
(Label1.Text.Length = 0)) Then
Label1.Text = Label1.Text & sender.text
End If
Case "C"
Label1.Text = ""
Case "CE"
If Label1.Text.Length > 0 Then
Label1.Text = Label1.Text.Substring(0, Label1.Text.Length - 1)
End If
Case "="
' parse the expression...
End Select
End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Butto
Friend WithEvents Button2 As System.Windows.Forms.Butto
Friend WithEvents Button3 As System.Windows.Forms.Butto
Friend WithEvents Button4 As System.Windows.Forms.Butto
Friend WithEvents Button5 As System.Windows.Forms.Butto
Friend WithEvents Button6 As System.Windows.Forms.Butto
Friend WithEvents Button7 As System.Windows.Forms.Butto
Friend WithEvents Button8 As System.Windows.Forms.Butto
Friend WithEvents Button9 As System.Windows.Forms.Butto
Friend WithEvents Button17 As System.Windows.Forms.Butto
Friend WithEvents Button18 As System.Windows.Forms.Butto
Friend WithEvents Button11 As System.Windows.Forms.Butto
Friend WithEvents Button12 As System.Windows.Forms.Butto
Friend WithEvents Button13 As System.Windows.Forms.Butto
Friend WithEvents Button14 As System.Windows.Forms.Butto
Friend WithEvents Button10 As System.Windows.Forms.Butto
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Button19 As System.Windows.Forms.Butto
Friend WithEvents Button15 As System.Windows.Forms.Butto
Friend WithEvents Button16 As System.Windows.Forms.Butto
<System.Diagnostics.Debugg
Me.Button1 = New System.Windows.Forms.Butto
Me.Button2 = New System.Windows.Forms.Butto
Me.Button3 = New System.Windows.Forms.Butto
Me.Button4 = New System.Windows.Forms.Butto
Me.Button5 = New System.Windows.Forms.Butto
Me.Button6 = New System.Windows.Forms.Butto
Me.Button7 = New System.Windows.Forms.Butto
Me.Button8 = New System.Windows.Forms.Butto
Me.Button9 = New System.Windows.Forms.Butto
Me.Button17 = New System.Windows.Forms.Butto
Me.Button18 = New System.Windows.Forms.Butto
Me.Button11 = New System.Windows.Forms.Butto
Me.Button12 = New System.Windows.Forms.Butto
Me.Button13 = New System.Windows.Forms.Butto
Me.Button14 = New System.Windows.Forms.Butto
Me.Button10 = New System.Windows.Forms.Butto
Me.Label1 = New System.Windows.Forms.Label
Me.Button19 = New System.Windows.Forms.Butto
Me.Button15 = New System.Windows.Forms.Butto
Me.Button16 = New System.Windows.Forms.Butto
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 40)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(32, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "7"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(48, 40)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(32, 32)
Me.Button2.TabIndex = 1
Me.Button2.Text = "8"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(88, 40)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(32, 32)
Me.Button3.TabIndex = 2
Me.Button3.Text = "9"
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(8, 80)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(32, 32)
Me.Button4.TabIndex = 3
Me.Button4.Text = "4"
'
'Button5
'
Me.Button5.Location = New System.Drawing.Point(48, 80)
Me.Button5.Name = "Button5"
Me.Button5.Size = New System.Drawing.Size(32, 32)
Me.Button5.TabIndex = 4
Me.Button5.Text = "5"
'
'Button6
'
Me.Button6.Location = New System.Drawing.Point(88, 80)
Me.Button6.Name = "Button6"
Me.Button6.Size = New System.Drawing.Size(32, 32)
Me.Button6.TabIndex = 5
Me.Button6.Text = "6"
'
'Button7
'
Me.Button7.Location = New System.Drawing.Point(8, 120)
Me.Button7.Name = "Button7"
Me.Button7.Size = New System.Drawing.Size(32, 32)
Me.Button7.TabIndex = 6
Me.Button7.Text = "1"
'
'Button8
'
Me.Button8.Location = New System.Drawing.Point(48, 120)
Me.Button8.Name = "Button8"
Me.Button8.Size = New System.Drawing.Size(32, 32)
Me.Button8.TabIndex = 7
Me.Button8.Text = "2"
'
'Button9
'
Me.Button9.Location = New System.Drawing.Point(88, 120)
Me.Button9.Name = "Button9"
Me.Button9.Size = New System.Drawing.Size(32, 32)
Me.Button9.TabIndex = 8
Me.Button9.Text = "3"
'
'Button17
'
Me.Button17.Location = New System.Drawing.Point(8, 200)
Me.Button17.Name = "Button17"
Me.Button17.Size = New System.Drawing.Size(32, 32)
Me.Button17.TabIndex = 9
Me.Button17.Text = "C"
'
'Button18
'
Me.Button18.Location = New System.Drawing.Point(48, 200)
Me.Button18.Name = "Button18"
Me.Button18.Size = New System.Drawing.Size(32, 32)
Me.Button18.TabIndex = 10
Me.Button18.Text = "CE"
'
'Button11
'
Me.Button11.Location = New System.Drawing.Point(128, 40)
Me.Button11.Name = "Button11"
Me.Button11.Size = New System.Drawing.Size(32, 32)
Me.Button11.TabIndex = 11
Me.Button11.Text = "/"
'
'Button12
'
Me.Button12.Location = New System.Drawing.Point(128, 80)
Me.Button12.Name = "Button12"
Me.Button12.Size = New System.Drawing.Size(32, 32)
Me.Button12.TabIndex = 12
Me.Button12.Text = "X"
'
'Button13
'
Me.Button13.Location = New System.Drawing.Point(128, 120)
Me.Button13.Name = "Button13"
Me.Button13.Size = New System.Drawing.Size(32, 32)
Me.Button13.TabIndex = 13
Me.Button13.Text = "-"
'
'Button14
'
Me.Button14.Location = New System.Drawing.Point(128, 160)
Me.Button14.Name = "Button14"
Me.Button14.Size = New System.Drawing.Size(32, 32)
Me.Button14.TabIndex = 14
Me.Button14.Text = "+"
'
'Button10
'
Me.Button10.Location = New System.Drawing.Point(48, 160)
Me.Button10.Name = "Button10"
Me.Button10.Size = New System.Drawing.Size(32, 32)
Me.Button10.TabIndex = 15
Me.Button10.Text = "0"
'
'Label1
'
Me.Label1.Anchor = CType(((System.Windows.For
Or System.Windows.Forms.Ancho
Me.Label1.BorderStyle = System.Windows.Forms.Borde
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(152, 24)
Me.Label1.TabIndex = 16
Me.Label1.TextAlign = System.Drawing.ContentAlig
'
'Button19
'
Me.Button19.Location = New System.Drawing.Point(88, 200)
Me.Button19.Name = "Button19"
Me.Button19.Size = New System.Drawing.Size(72, 32)
Me.Button19.TabIndex = 19
Me.Button19.Text = "="
'
'Button15
'
Me.Button15.Location = New System.Drawing.Point(8, 160)
Me.Button15.Name = "Button15"
Me.Button15.Size = New System.Drawing.Size(32, 32)
Me.Button15.TabIndex = 20
Me.Button15.Text = "("
'
'Button16
'
Me.Button16.Location = New System.Drawing.Point(88, 160)
Me.Button16.Name = "Button16"
Me.Button16.Size = New System.Drawing.Size(32, 32)
Me.Button16.TabIndex = 21
Me.Button16.Text = ")"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(168, 238)
Me.Controls.Add(Me.Button1
Me.Controls.Add(Me.Button1
Me.Controls.Add(Me.Button1
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button1
Me.Controls.Add(Me.Button1
Me.Controls.Add(Me.Button1
Me.Controls.Add(Me.Button1
Me.Controls.Add(Me.Button1
Me.Controls.Add(Me.Button1
Me.Controls.Add(Me.Button1
Me.Controls.Add(Me.Button9
Me.Controls.Add(Me.Button8
Me.Controls.Add(Me.Button7
Me.Controls.Add(Me.Button6
Me.Controls.Add(Me.Button5
Me.Controls.Add(Me.Button4
Me.Controls.Add(Me.Button3
Me.Controls.Add(Me.Button2
Me.Controls.Add(Me.Button1
Me.Name = "Form1"
Me.Text = "Calculator"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button_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, _
Button10.Click, Button11.Click, Button12.Click, Button13.Click, Button14.Click, _
Button15.Click, Button16.Click, Button17.Click, Button18.Click
Select Case sender.text
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "(", ")"
Label1.Text = Label1.Text & sender.text
Case "/", "X", "-", "+"
If Not (Label1.Text.EndsWith("/")
Label1.Text.EndsWith("-") Or Label1.Text.EndsWith("+") Or _
(Label1.Text.Length = 0)) Then
Label1.Text = Label1.Text & sender.text
End If
Case "C"
Label1.Text = ""
Case "CE"
If Label1.Text.Length > 0 Then
Label1.Text = Label1.Text.Substring(0, Label1.Text.Length - 1)
End If
Case "="
' parse the expression...
End Select
End Sub
End Class
The second part of your question is not so easy. What you are asking for is an expression evaluator:
3+4-1+2/2
Their are many things involved in doing this calculation. You first need to define your operator precedence. Is it simply left to right, or should multiplication and division be done before addition and subtraction? Will you allow grouping with parentheses?
~IM
3+4-1+2/2
Their are many things involved in doing this calculation. You first need to define your operator precedence. Is it simply left to right, or should multiplication and division be done before addition and subtraction? Will you allow grouping with parentheses?
~IM
Here is a website that explains how to convert expressions from infix to postfix, and how to evaluate postfix (reverse polish notation or RPN) expressions:
http://www.spsu.edu/cs/faculty/bbrown/web_lectures/postfix/
Writing the code for this is not a 50 point question.
This is a common homework assignment in college courses. If this is homework, what progress have you made so far? (Apologies if this is not homework.)
~IM
http://www.spsu.edu/cs/faculty/bbrown/web_lectures/postfix/
Writing the code for this is not a 50 point question.
This is a common homework assignment in college courses. If this is homework, what progress have you made so far? (Apologies if this is not homework.)
~IM
ASKER
The first part you gave worked great, Thanks!
This is homework, and this is what I have for the equal button: (txtDisplay2.text is a running tape on the side of the calc.) Right now I am testing the subtraction button, the program dies on line result = CDbl(num1) - CDbl(num2) ) but I thought that the next line txtDisplay.Text = CStr(result) would change the double result "6-3" to type string ???
This is homework, and this is what I have for the equal button:
Private Sub btnEquals_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEquals.Click
num1 = txtDisplay.Text
'num2 = txtDisplay2.Text
If Len(txtDisplay.Text) > 0 Then
Select Case symbol
Case "+"
result = CDbl(num1) + CDbl(num2)
txtDisplay.Text = CStr(result)
'txtDisplay2.Text = CStr(result)
Case "-"
result = CDbl(num1) - CDbl(num2) ****program dies on this line ** right now I'm only testing the subtraction button
txtDisplay.Text = CStr(result)
Case "X"
result = CDbl(num1) * CDbl(num2)
txtDisplay.Text = CStr(result)
Case "\"
If num2 <> 0 Then
result = CDbl(num1) / CDbl(num2)
txtDisplay.Text = CStr(result)
Else
MsgBox("Divide Error")
End If
End Select
Else
MsgBox("Not enough Arguments!")
End If
End Sub
Then for the subtraction button I have:
Private Sub btnSub_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSub.Click
symbol = "-"
num1 = txtDisplay.Text
End Sub
This is homework, and this is what I have for the equal button: (txtDisplay2.text is a running tape on the side of the calc.) Right now I am testing the subtraction button, the program dies on line result = CDbl(num1) - CDbl(num2) ) but I thought that the next line txtDisplay.Text = CStr(result) would change the double result "6-3" to type string ???
This is homework, and this is what I have for the equal button:
Private Sub btnEquals_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEquals.Click
num1 = txtDisplay.Text
'num2 = txtDisplay2.Text
If Len(txtDisplay.Text) > 0 Then
Select Case symbol
Case "+"
result = CDbl(num1) + CDbl(num2)
txtDisplay.Text = CStr(result)
'txtDisplay2.Text = CStr(result)
Case "-"
result = CDbl(num1) - CDbl(num2) ****program dies on this line ** right now I'm only testing the subtraction button
txtDisplay.Text = CStr(result)
Case "X"
result = CDbl(num1) * CDbl(num2)
txtDisplay.Text = CStr(result)
Case "\"
If num2 <> 0 Then
result = CDbl(num1) / CDbl(num2)
txtDisplay.Text = CStr(result)
Else
MsgBox("Divide Error")
End If
End Select
Else
MsgBox("Not enough Arguments!")
End If
End Sub
Then for the subtraction button I have:
Private Sub btnSub_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSub.Click
symbol = "-"
num1 = txtDisplay.Text
End Sub
You have commented out the num2 line so it doesn't have a value. This is probably why it bombs when you try to convert it to a double using CDbl().
Where is symbol coming from in the Select Case statement?
Are you going to evaluate complex expressions like the ones you posted?
3+4-1+2/2
<or>
1+1+1+8+4
Or are you only going to have two values and one operator in each expression?
~IM
Where is symbol coming from in the Select Case statement?
Are you going to evaluate complex expressions like the ones you posted?
3+4-1+2/2
<or>
1+1+1+8+4
Or are you only going to have two values and one operator in each expression?
~IM
ASKER
I had taken out the commented line and still got the same error ??
The calculator should be able to do complex expressions 3+4-1+2/2
The symbol is coming from the btn click event: This is for the subtraction symbol:
Private Sub btnSub_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSub.Click
symbol = "-"
num1 = txtDisplay.Text
End Sub
Also I declared the num1 and num2 as Object, should this be integer?
Thanks!
The calculator should be able to do complex expressions 3+4-1+2/2
The symbol is coming from the btn click event: This is for the subtraction symbol:
Private Sub btnSub_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSub.Click
symbol = "-"
num1 = txtDisplay.Text
End Sub
Also I declared the num1 and num2 as Object, should this be integer?
Thanks!
num1 and num2 should be Strings since you are assigning the .Text property to them. Result should be a Double since you are using CDbl().
The main problem though, is that your approach won't be able to handle complex expressions since symbol is only going to have the last operation clicked but you may have more than one operation in your expressions. You can't compute the answer as it is entered since multiplication and division should be done before addition and subtraction. You must wait until the entire expression is entered and then parse it.
The correct way to do it is to use the techniques outlined in the link I provided you earlier.
Is the user supposed to enter the whole expression at once or is it supposed to work like a normal calculator?
5+1=6
would be:
5 (5 in display)
+ (5 in display)
1 (1 in display)
= (6 in display)
Sorry if I have misunderstood the requirements...
~IM
The main problem though, is that your approach won't be able to handle complex expressions since symbol is only going to have the last operation clicked but you may have more than one operation in your expressions. You can't compute the answer as it is entered since multiplication and division should be done before addition and subtraction. You must wait until the entire expression is entered and then parse it.
The correct way to do it is to use the techniques outlined in the link I provided you earlier.
Is the user supposed to enter the whole expression at once or is it supposed to work like a normal calculator?
5+1=6
would be:
5 (5 in display)
+ (5 in display)
1 (1 in display)
= (6 in display)
Sorry if I have misunderstood the requirements...
~IM
ASKER
The requirements call for 5+1-2 in txtDisplay.text (not like a normal calc)
press equal button to force out last calculation
result displayed in txtDisplay2.text as
5+
1+
6
2-
4
txtDisplay2.text is acting as a running tape.
press equal button to force out last calculation
result displayed in txtDisplay2.text as
5+
1+
6
2-
4
txtDisplay2.text is acting as a running tape.
Ah! ok...it's clear to me now. It's a new fangled hybrid calculator. ;)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This what you are looking for?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox2.Text <> "" Then
TextBox2.AppendText(" + " & TextBox1.Text)
Else
TextBox2.Text = TextBox1.Text
End If
End Sub