Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Appending two variables

Posted on 2004-10-31
12
Medium Priority
?
259 Views
Last Modified: 2010-04-23
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!
0
Comment
Question by:itcopt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
12 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12460513
Not quite sure what you are asking here....

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
0
 

Author Comment

by:itcopt
ID: 12463864
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.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12466349
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.IContainer

    '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.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents Button4 As System.Windows.Forms.Button
    Friend WithEvents Button5 As System.Windows.Forms.Button
    Friend WithEvents Button6 As System.Windows.Forms.Button
    Friend WithEvents Button7 As System.Windows.Forms.Button
    Friend WithEvents Button8 As System.Windows.Forms.Button
    Friend WithEvents Button9 As System.Windows.Forms.Button
    Friend WithEvents Button17 As System.Windows.Forms.Button
    Friend WithEvents Button18 As System.Windows.Forms.Button
    Friend WithEvents Button11 As System.Windows.Forms.Button
    Friend WithEvents Button12 As System.Windows.Forms.Button
    Friend WithEvents Button13 As System.Windows.Forms.Button
    Friend WithEvents Button14 As System.Windows.Forms.Button
    Friend WithEvents Button10 As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Button19 As System.Windows.Forms.Button
    Friend WithEvents Button15 As System.Windows.Forms.Button
    Friend WithEvents Button16 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Button3 = New System.Windows.Forms.Button
        Me.Button4 = New System.Windows.Forms.Button
        Me.Button5 = New System.Windows.Forms.Button
        Me.Button6 = New System.Windows.Forms.Button
        Me.Button7 = New System.Windows.Forms.Button
        Me.Button8 = New System.Windows.Forms.Button
        Me.Button9 = New System.Windows.Forms.Button
        Me.Button17 = New System.Windows.Forms.Button
        Me.Button18 = New System.Windows.Forms.Button
        Me.Button11 = New System.Windows.Forms.Button
        Me.Button12 = New System.Windows.Forms.Button
        Me.Button13 = New System.Windows.Forms.Button
        Me.Button14 = New System.Windows.Forms.Button
        Me.Button10 = New System.Windows.Forms.Button
        Me.Label1 = New System.Windows.Forms.Label
        Me.Button19 = New System.Windows.Forms.Button
        Me.Button15 = New System.Windows.Forms.Button
        Me.Button16 = New System.Windows.Forms.Button
        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.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        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.ContentAlignment.MiddleRight
        '
        '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.Button16)
        Me.Controls.Add(Me.Button15)
        Me.Controls.Add(Me.Button19)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Button10)
        Me.Controls.Add(Me.Button14)
        Me.Controls.Add(Me.Button13)
        Me.Controls.Add(Me.Button12)
        Me.Controls.Add(Me.Button11)
        Me.Controls.Add(Me.Button18)
        Me.Controls.Add(Me.Button17)
        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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12466387
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
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12466511
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

0
 

Author Comment

by:itcopt
ID: 12473379
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
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12473745
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
0
 

Author Comment

by:itcopt
ID: 12474570
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!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12474736
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
0
 

Author Comment

by:itcopt
ID: 12476127
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.

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12476721
Ah! ok...it's clear to me now.  It's a new fangled hybrid calculator.  ;)
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 600 total points
ID: 12478798
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.IContainer

    '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 btn7 As System.Windows.Forms.Button
    Friend WithEvents btn8 As System.Windows.Forms.Button
    Friend WithEvents btn9 As System.Windows.Forms.Button
    Friend WithEvents btn4 As System.Windows.Forms.Button
    Friend WithEvents btn5 As System.Windows.Forms.Button
    Friend WithEvents btn6 As System.Windows.Forms.Button
    Friend WithEvents btn1 As System.Windows.Forms.Button
    Friend WithEvents btn2 As System.Windows.Forms.Button
    Friend WithEvents btn3 As System.Windows.Forms.Button
    Friend WithEvents btnClear As System.Windows.Forms.Button
    Friend WithEvents btnClearEntry As System.Windows.Forms.Button
    Friend WithEvents btnDivide As System.Windows.Forms.Button
    Friend WithEvents btnMultiplay As System.Windows.Forms.Button
    Friend WithEvents btnSubtract As System.Windows.Forms.Button
    Friend WithEvents btnAdd As System.Windows.Forms.Button
    Friend WithEvents btnEquals As System.Windows.Forms.Button
    Friend WithEvents txtDisplay As System.Windows.Forms.Label
    Friend WithEvents btn0 As System.Windows.Forms.Button
    Friend WithEvents txtDisplay2 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btn7 = New System.Windows.Forms.Button
        Me.btn8 = New System.Windows.Forms.Button
        Me.btn9 = New System.Windows.Forms.Button
        Me.btn4 = New System.Windows.Forms.Button
        Me.btn5 = New System.Windows.Forms.Button
        Me.btn6 = New System.Windows.Forms.Button
        Me.btn1 = New System.Windows.Forms.Button
        Me.btn2 = New System.Windows.Forms.Button
        Me.btn3 = New System.Windows.Forms.Button
        Me.btnClear = New System.Windows.Forms.Button
        Me.btnClearEntry = New System.Windows.Forms.Button
        Me.btnDivide = New System.Windows.Forms.Button
        Me.btnMultiplay = New System.Windows.Forms.Button
        Me.btnSubtract = New System.Windows.Forms.Button
        Me.btnAdd = New System.Windows.Forms.Button
        Me.btn0 = New System.Windows.Forms.Button
        Me.btnEquals = New System.Windows.Forms.Button
        Me.txtDisplay = New System.Windows.Forms.Label
        Me.txtDisplay2 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'btn7
        '
        Me.btn7.Location = New System.Drawing.Point(8, 48)
        Me.btn7.Name = "btn7"
        Me.btn7.Size = New System.Drawing.Size(32, 32)
        Me.btn7.TabIndex = 0
        Me.btn7.Text = "7"
        '
        'btn8
        '
        Me.btn8.Location = New System.Drawing.Point(48, 48)
        Me.btn8.Name = "btn8"
        Me.btn8.Size = New System.Drawing.Size(32, 32)
        Me.btn8.TabIndex = 1
        Me.btn8.Text = "8"
        '
        'btn9
        '
        Me.btn9.Location = New System.Drawing.Point(88, 48)
        Me.btn9.Name = "btn9"
        Me.btn9.Size = New System.Drawing.Size(32, 32)
        Me.btn9.TabIndex = 2
        Me.btn9.Text = "9"
        '
        'btn4
        '
        Me.btn4.Location = New System.Drawing.Point(8, 88)
        Me.btn4.Name = "btn4"
        Me.btn4.Size = New System.Drawing.Size(32, 32)
        Me.btn4.TabIndex = 3
        Me.btn4.Text = "4"
        '
        'btn5
        '
        Me.btn5.Location = New System.Drawing.Point(48, 88)
        Me.btn5.Name = "btn5"
        Me.btn5.Size = New System.Drawing.Size(32, 32)
        Me.btn5.TabIndex = 4
        Me.btn5.Text = "5"
        '
        'btn6
        '
        Me.btn6.Location = New System.Drawing.Point(88, 88)
        Me.btn6.Name = "btn6"
        Me.btn6.Size = New System.Drawing.Size(32, 32)
        Me.btn6.TabIndex = 5
        Me.btn6.Text = "6"
        '
        'btn1
        '
        Me.btn1.Location = New System.Drawing.Point(8, 128)
        Me.btn1.Name = "btn1"
        Me.btn1.Size = New System.Drawing.Size(32, 32)
        Me.btn1.TabIndex = 6
        Me.btn1.Text = "1"
        '
        'btn2
        '
        Me.btn2.Location = New System.Drawing.Point(48, 128)
        Me.btn2.Name = "btn2"
        Me.btn2.Size = New System.Drawing.Size(32, 32)
        Me.btn2.TabIndex = 7
        Me.btn2.Text = "2"
        '
        'btn3
        '
        Me.btn3.Location = New System.Drawing.Point(88, 128)
        Me.btn3.Name = "btn3"
        Me.btn3.Size = New System.Drawing.Size(32, 32)
        Me.btn3.TabIndex = 8
        Me.btn3.Text = "3"
        '
        'btnClear
        '
        Me.btnClear.Location = New System.Drawing.Point(8, 168)
        Me.btnClear.Name = "btnClear"
        Me.btnClear.Size = New System.Drawing.Size(32, 32)
        Me.btnClear.TabIndex = 9
        Me.btnClear.Text = "C"
        '
        'btnClearEntry
        '
        Me.btnClearEntry.Location = New System.Drawing.Point(88, 168)
        Me.btnClearEntry.Name = "btnClearEntry"
        Me.btnClearEntry.Size = New System.Drawing.Size(32, 32)
        Me.btnClearEntry.TabIndex = 10
        Me.btnClearEntry.Text = "CE"
        '
        'btnDivide
        '
        Me.btnDivide.Location = New System.Drawing.Point(128, 48)
        Me.btnDivide.Name = "btnDivide"
        Me.btnDivide.Size = New System.Drawing.Size(32, 32)
        Me.btnDivide.TabIndex = 11
        Me.btnDivide.Text = "/"
        '
        'btnMultiplay
        '
        Me.btnMultiplay.Location = New System.Drawing.Point(128, 88)
        Me.btnMultiplay.Name = "btnMultiplay"
        Me.btnMultiplay.Size = New System.Drawing.Size(32, 32)
        Me.btnMultiplay.TabIndex = 12
        Me.btnMultiplay.Text = "x"
        '
        'btnSubtract
        '
        Me.btnSubtract.Location = New System.Drawing.Point(128, 128)
        Me.btnSubtract.Name = "btnSubtract"
        Me.btnSubtract.Size = New System.Drawing.Size(32, 32)
        Me.btnSubtract.TabIndex = 13
        Me.btnSubtract.Text = "-"
        '
        'btnAdd
        '
        Me.btnAdd.Location = New System.Drawing.Point(128, 168)
        Me.btnAdd.Name = "btnAdd"
        Me.btnAdd.Size = New System.Drawing.Size(32, 32)
        Me.btnAdd.TabIndex = 14
        Me.btnAdd.Text = "+"
        '
        'btn0
        '
        Me.btn0.Location = New System.Drawing.Point(48, 168)
        Me.btn0.Name = "btn0"
        Me.btn0.Size = New System.Drawing.Size(32, 32)
        Me.btn0.TabIndex = 15
        Me.btn0.Text = "0"
        '
        'btnEquals
        '
        Me.btnEquals.Location = New System.Drawing.Point(8, 208)
        Me.btnEquals.Name = "btnEquals"
        Me.btnEquals.Size = New System.Drawing.Size(152, 32)
        Me.btnEquals.TabIndex = 19
        Me.btnEquals.Text = "="
        '
        'txtDisplay
        '
        Me.txtDisplay.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtDisplay.Location = New System.Drawing.Point(8, 8)
        Me.txtDisplay.Name = "txtDisplay"
        Me.txtDisplay.Size = New System.Drawing.Size(152, 32)
        Me.txtDisplay.TabIndex = 22
        Me.txtDisplay.Text = "0"
        Me.txtDisplay.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'txtDisplay2
        '
        Me.txtDisplay2.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.txtDisplay2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtDisplay2.Location = New System.Drawing.Point(168, 8)
        Me.txtDisplay2.Multiline = True
        Me.txtDisplay2.Name = "txtDisplay2"
        Me.txtDisplay2.ReadOnly = True
        Me.txtDisplay2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtDisplay2.Size = New System.Drawing.Size(152, 232)
        Me.txtDisplay2.TabIndex = 23
        Me.txtDisplay2.Text = ""
        Me.txtDisplay2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(328, 246)
        Me.Controls.Add(Me.txtDisplay2)
        Me.Controls.Add(Me.txtDisplay)
        Me.Controls.Add(Me.btnEquals)
        Me.Controls.Add(Me.btn0)
        Me.Controls.Add(Me.btnAdd)
        Me.Controls.Add(Me.btnSubtract)
        Me.Controls.Add(Me.btnMultiplay)
        Me.Controls.Add(Me.btnDivide)
        Me.Controls.Add(Me.btnClearEntry)
        Me.Controls.Add(Me.btnClear)
        Me.Controls.Add(Me.btn3)
        Me.Controls.Add(Me.btn2)
        Me.Controls.Add(Me.btn1)
        Me.Controls.Add(Me.btn6)
        Me.Controls.Add(Me.btn5)
        Me.Controls.Add(Me.btn4)
        Me.Controls.Add(Me.btn9)
        Me.Controls.Add(Me.btn8)
        Me.Controls.Add(Me.btn7)
        Me.Name = "Form1"
        Me.Text = "Calculator"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private myStack As Stack = New Stack

    Private Sub Button_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles btn7.Click, btn8.Click, btn9.Click, btn4.Click, _
        btn5.Click, btn6.Click, btn1.Click, btn2.Click, btn3.Click, _
        btnClear.Click, btnClearEntry.Click, btnDivide.Click, _
        btnMultiplay.Click, btnSubtract.Click, btnAdd.Click, btn0.Click, _
        btnEquals.Click

        Dim result As Double
        Select Case sender.text
            Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
                If txtDisplay.Text = "0" Then
                    txtDisplay.Text = sender.text
                Else
                    txtDisplay.Text = txtDisplay.Text & sender.text
                End If

            Case "+", "-", "x", "/"
                Select Case myStack.Count
                    Case 0
                        myStack.Push(txtDisplay.Text)
                        myStack.Push(sender.text)
                        txtDisplay2.AppendText(txtDisplay.Text & vbCrLf & sender.text)

                    Case 1
                        txtDisplay2.AppendText(sender.text)
                        myStack.Push(sender.text)

                    Case 2
                        txtDisplay2.AppendText(txtDisplay.Text & vbCrLf)
                        myStack.Push(txtDisplay.Text)
                        result = getResult()
                        txtDisplay2.AppendText("=" & CStr(result) & vbCrLf)
                        myStack.Push(CStr(result))
                        myStack.Push(sender.text)
                        txtDisplay2.AppendText(sender.text)
                End Select
                txtDisplay.Text = "0"

            Case "="
                Debug.WriteLine(myStack.Count)
                If myStack.Count = 2 Then
                    txtDisplay2.AppendText(txtDisplay.Text & vbCrLf)
                    myStack.Push(txtDisplay.Text)
                    result = getResult()
                    txtDisplay2.AppendText("=" & CStr(result) & vbCrLf)
                    txtDisplay.Text = "0"
                    myStack.Push(CStr(result))
                End If

            Case "C"
                txtDisplay.Text = "0"
                txtDisplay2.Text = ""
                myStack.Clear()

            Case "CE"
                If txtDisplay.Text.Length > 1 Then
                    txtDisplay.Text = txtDisplay.Text.Substring(0, txtDisplay.Text.Length - 1)
                Else
                    txtDisplay.Text = "0"
                End If
        End Select
    End Sub

    Private Function getResult() As Double
        Dim operand1 As Double
        Dim operand2 As Double
        Dim operator As String

        operand2 = Double.Parse(myStack.Pop)
        operator = myStack.Pop
        operand1 = Double.Parse(myStack.Pop)
        Select Case operator
            Case "+"
                Return operand1 + operand2

            Case "-"
                Return operand1 - operand2

            Case "x"
                Return operand1 * operand2

            Case "/"
                Return operand1 / operand2

        End Select
    End Function

End Class
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Loops Section Overview

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question