Hi all-

I'm working on an assignment for class and I would like some help. I have to create a calculator. In order to do the calculation, when the math operator is entered, there is a space on either side, so that I can split by the white spaces.

Private Sub btnEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEqual.Click
        Dim splitNumbers As String
        splitNumbers=txtCompute.Text.Split(" ")
        Dim numberOne, numberTwo, mathOperator As String
        numberOne= CDbl(splitNumbers(0))
        mathOperator=splitNumbers(1)
        numberTwo=CDbl(splitNumbers(2))
    End Sub

This was my original idea of how I could accomplish my goal, however, I get an error message saying that splitNumbers cannot be of type string. Do I have to create an array to do this or is there an easier way than what I'm seeing?

Thank you in advance!!

Recommended Answers

All 7 Replies

"Split" takes a string and splits it into multiple strings so the result has to go into a string array as follows

dim splitNumbers() as String = txtCompute.Text.Split(" ")

Thank you! That helped a lot. I've gotten everything to work on this thing, except for the addition. Instead of adding the numbers together, it concants them. Ex) 9+9=99.

Dim total As Double
        Select Case mathOperator
            Case Is = "+"
                total = numberOne + numberTwo
            Case Is = "-"
                total = numberOne - numberTwo
            Case Is = "/"
                total = numberOne / numberTwo
            Case Is = "*"
                total = numberOne * numberTwo
        End Select

All of the other math operations work correctly, but I can't figure out why the addition is doing this.

Did you declare numberOne and numberTwo as strings?

PS: What happens when the string contains 2 or more operators?

You can convert strings to numbers using CDBL as in

total = CDbl(numberOne) + CDbl(numberTwo)

You can either verify that numberOne and numberTwo are valid numbers before the select statement, or you can enclose the select clause in a Try/Catch block. Don't assume that the user will enter only valid numbers.

One suggestion for handling multiple operators such as a + b + c would be to calculate a + b then replace a + b with the result, reparse and repeat until you are down to a single term which would be the result.

<img src="/images/attachments/3/calculator.jpg" title="">
Option Explicit On
Imports System

Public Class MainForm1
    Public curval As Double
    Public pre_curval As Double
    Public preval As Double
    Public result As Double
    Public choice As String
    Private nonNumberEntered As Boolean = False

    Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
        nonNumberEntered = False
        If (e.KeyCode < 48 OrElse e.KeyCode > 58) And e.KeyCode <> 8 And e.KeyCode <> 46 Then
            If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
                If e.KeyCode <> Keys.Back Then
                    nonNumberEntered = True
                End If
            End If
        End If
        '    Beep()
        '    nonNumberEntered = True
        'End If
        'If Len(RichTextBox1.Text) > 10 Then
        '    Beep()
        '    nonNumberEntered = True
        'End If
        'If e.KeyCode = 46 Then
        '    If InStr(1, RichTextBox1.Text, ".") > 0 Then
        '        Beep()
        '        nonNumberEntered = True
        '    End If
        'End If

    End Sub
    Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress
        If nonNumberEntered = True Then
            e.Handled = True
        End If
    End Sub
    Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
        If RichTextBox1.SelectedText <> "" Then
            RichTextBox1.Cut()
        End If
    End Sub
    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        If RichTextBox1.SelectionLength > 0 Then
            RichTextBox1.Copy()
        End If
    End Sub
    Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
        If Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) = True Then
            If RichTextBox1.SelectionLength > 0 Then
                If MessageBox.Show("Do you want to paste over current selection?", _
                    "Cut Example", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
                    RichTextBox1.SelectionStart = RichTextBox1.SelectionStart + _
                        RichTextBox1.SelectionLength
                End If
            End If
            RichTextBox1.Paste()
        End If
    End Sub
    Private Sub DigitGroupingToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End Sub
    Private Sub HelpTopicsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpTopicsToolStripMenuItem.Click
        HelpForm2.Show()
        Dim Msg As String
        Err.Clear()
        On Error Resume Next
        Err.Raise(6)
        If Err.Number <> 0 Then
            Msg = "Press F1 or HELP to see " & Err.HelpFile & " topic for" & _
            " the following HelpContext: " & Err.HelpContext
            MsgBox(Msg, , "Error:")
        End If
    End Sub
    Private Sub AboutCalculatorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutCalculatorToolStripMenuItem.Click
        AboutBox1.Show()
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RichTextBox1.Text = RichTextBox1.Text + Button1.Text
        curval = Val(RichTextBox1.Text)
        pre_curval = Val(RichTextBox1.Text)
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        RichTextBox1.Text = RichTextBox1.Text + Button2.Text
        curval = Val(RichTextBox1.Text)
        pre_curval = Val(RichTextBox1.Text)
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        RichTextBox1.Text = RichTextBox1.Text + Button3.Text
        curval = Val(RichTextBox1.Text)
        pre_curval = Val(RichTextBox1.Text)
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        RichTextBox1.Text = RichTextBox1.Text + Button4.Text
        curval = Val(RichTextBox1.Text)
        pre_curval = Val(RichTextBox1.Text)
    End Sub
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        RichTextBox1.Text = RichTextBox1.Text + Button5.Text
        curval = Val(RichTextBox1.Text)
        pre_curval = Val(RichTextBox1.Text)
    End Sub
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        RichTextBox1.Text = RichTextBox1.Text + Button6.Text
        curval = Val(RichTextBox1.Text)
        pre_curval = Val(RichTextBox1.Text)
    End Sub
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        RichTextBox1.Text = RichTextBox1.Text + Button7.Text
        curval = Val(RichTextBox1.Text)
        pre_curval = Val(RichTextBox1.Text)
    End Sub
    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        RichTextBox1.Text = RichTextBox1.Text + Button8.Text
        curval = Val(RichTextBox1.Text)
        pre_curval = Val(RichTextBox1.Text)
    End Sub
    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        RichTextBox1.Text = RichTextBox1.Text + Button9.Text
        curval = Val(RichTextBox1.Text)
        pre_curval = Val(RichTextBox1.Text)
    End Sub
    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        If RichTextBox1.Text = "" Or RichTextBox1.Text = "0" Then
        Else
            RichTextBox1.Text = RichTextBox1.Text + Button10.Text
            curval = Val(RichTextBox1.Text)
            pre_curval = Val(RichTextBox1.Text)
        End If
    End Sub
    Private Sub ButtonDot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDot.Click
        If RichTextBox1.Text.Contains(".") Then
        Else
            RichTextBox1.Text = RichTextBox1.Text + ButtonDot.Text
            curval = Val(RichTextBox1.Text)
        End If
    End Sub
    Private Sub ButtonAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAdd.Click
        RichTextBox1.Text = ""
        preval = curval
        curval = 0
        choice = "+"
        Label1.Text = Convert.ToString(preval) + choice
    End Sub
    Private Sub ButtonSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSub.Click
        RichTextBox1.Text = ""
        preval = curval
        curval = 0
        choice = "-"
        Label1.Text = Convert.ToString(preval) + choice
    End Sub
    Private Sub ButtonMul_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonMul.Click
        RichTextBox1.Text = ""
        preval = curval
        curval = 0
        choice = "*"
        Label1.Text = Convert.ToString(preval) + choice
    End Sub
    Private Sub ButtonDiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDiv.Click
        RichTextBox1.Text = ""
        preval = curval
        curval = 0
        choice = "/"
        Label1.Text = Convert.ToString(preval) + choice
    End Sub
    Private Sub ButtonPlusMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPlusMinus.Click
        curval = -curval
        RichTextBox1.Text = Str(curval)
    End Sub

    Private Sub ButtonEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEqual.Click
        If Len(RichTextBox1.Text) > 0 Then
            If pre_curval <> vbNull Then
                Select Case choice
                    Case "++"
                        result = pre_curval + curval
                        RichTextBox1.Text = Str(result)
                        Label1.Text = choice + " " + Convert.ToString(pre_curval)
                    Case "--"
                        result = curval - pre_curval
                        RichTextBox1.Text = Str(result)
                        Label1.Text = choice + " " + Convert.ToString(pre_curval)
                    Case "**"
                        result = pre_curval * curval
                        RichTextBox1.Text = Str(result)
                        Label1.Text = choice + " " + Convert.ToString(pre_curval)
                    Case "//"
                        If choice = "%" Then
                            result = result * 100
                            RichTextBox1.Text = result
                        End If
                        result = curval / pre_curval
                        RichTextBox1.Text = Str(result)
                        Label1.Text = choice + " " + Convert.ToString(pre_curval)
                End Select
            End If
            Select Case choice
                Case "+"
                    result = preval + curval
                    RichTextBox1.Text = Str(result)
                    Label1.Text = Convert.ToString(preval) + choice + Convert.ToString(curval)
                    choice = "++"
                Case "-"
                    result = preval - curval
                    RichTextBox1.Text = Str(result)
                    Label1.Text = Convert.ToString(preval) + choice + Convert.ToString(curval)
                    choice = "--"
                Case "*"
                    result = preval * curval
                    RichTextBox1.Text = Str(result)
                    Label1.Text = Convert.ToString(preval) + choice + Convert.ToString(curval)
                    choice = "**"
                Case "/"
                    If choice = "%" Then
                        result = result * 100
                        RichTextBox1.Text = result
                    End If
                    result = preval / curval
                    RichTextBox1.Text = Str(result)
                    Label1.Text = Convert.ToString(preval) + choice + Convert.ToString(curval)
                    choice = "**"
            End Select
            curval = result
        End If
    End Sub
    Private Sub ButtonSqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSqrt.Click
        Label1.Text = "sqrt(" + RichTextBox1.Text + ")"
        RichTextBox1.Text = Math.Sqrt(Val(RichTextBox1.Text))
        curval = preval = 0
    End Sub
    Private Sub ButtonPercentage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPercentage.Click
        If choice = "/" Then
            Label1.Text = Convert.ToString(preval) + "/" + Convert.ToString(curval) + "*100"
            result = preval / curval * 100
            RichTextBox1.Text = Str(result)
        End If
    End Sub
    Private Sub Button1byx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1byx.Click
        Label1.Text = "1/" + RichTextBox1.Text
        RichTextBox1.Text = 1 / Val(RichTextBox1.Text)
    End Sub
    Private Sub ButtonClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonClear.Click
        Label1.Text = ""
        RichTextBox1.Clear()
        TextBox1.Text = 0
        preval = 0
        curval = 0
        pre_curval = 0
    End Sub
    Private Sub ButtonCE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCE.Click
        curval = 0
        RichTextBox1.Clear()
    End Sub
    Private Sub ButtonBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBack.Click
        preval = curval
        If Len(RichTextBox1.Text) > 0 Then
            RichTextBox1.Text = Microsoft.VisualBasic.Left(RichTextBox1.Text, Len(RichTextBox1.Text) - 1)
        End If
        curval = Val(RichTextBox1.Text)
    End Sub
    Private Sub ButtonMADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonMADD.Click
        If Len(RichTextBox1.Text) > 0 Then
            TextBox1.Text = Val(TextBox1.Text) + Val(RichTextBox1.Text)
            ButtonFlat.Text = ""
            ButtonFlat.Text = "M"
        End If
    End Sub

    Private Sub ButtonMSUB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonMSUB.Click
        If Len(RichTextBox1.Text) > 0 Then
            TextBox1.Text = Val(TextBox1.Text) - Val(RichTextBox1.Text)
            ButtonFlat.Text = ""
            ButtonFlat.Text = "M"
        End If
    End Sub

    Private Sub ButtonMMUL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonMMUL.Click
        If Len(RichTextBox1.Text) > 0 Then
            TextBox1.Text = Val(TextBox1.Text) * Val(RichTextBox1.Text)
            ButtonFlat.Text = ""
            ButtonFlat.Text = "M"
        End If
    End Sub

    Private Sub ButtonMDIV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonMDIV.Click
        If Len(RichTextBox1.Text) > 0 Then
            TextBox1.Text = Val(TextBox1.Text) / Val(RichTextBox1.Text)
            ButtonFlat.Text = ""
            ButtonFlat.Text = "M"
        End If
    End Sub
    Private Sub ButtonMS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonMS.Click
        If Len(RichTextBox1.Text) > 0 Then
            TextBox1.Text = Val(RichTextBox1.Text)
            ButtonFlat.Text = "M"
        End If
    End Sub
    Private Sub ButtonMR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonMR.Click
        RichTextBox1.Text = Val(TextBox1.Text)
    End Sub
    Private Sub ButtonMC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonMC.Click
        TextBox1.Text = 0
        ButtonFlat.Text = ""
    End Sub
    Private Sub ButtonUndo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonUndo.Click
        RichTextBox1.Text = preval
        If RichTextBox1.CanUndo = True Then
            RichTextBox1.Undo()
        End If
    End Sub
    Private Sub UndoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem.Click
        RichTextBox1.Text = preval
        If RichTextBox1.CanUndo = True Then
            RichTextBox1.Undo()
        End If
    End Sub

    Private Sub MainForm1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        End
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Clipboard.Clear()
        RichTextBox1.SelectionAlignment = HorizontalAlignment.Right
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        End
    End Sub

    Private Sub ButtonFlat_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonFlat.TextChanged
        RichTextBox1.Text = ""
    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        If RichTextBox1.Text = "Infinity" Then
            MsgBox("Infinity", MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "Infinity Value")
            RichTextBox1.Text = ""
        End If
    End Sub
End Class

You didn't ask a question so I'm assuming you posted the code for comments. I have a couple of comments and a suggestion.

You should add whitespace to make the code more readable. Codethathasnowhitespaceismuchhardertoreadsopleaseuseit.

Your code has no comments. All programs should have a header with comments that give (minimally) the program name, a description of what the program does, and any components (non-standard) required to build/run the program. I like to include an audit trail that shows when the program was written and when modifications were done. My personal preference is to create the header as a string variable which I can use in the "about" box. Each sub/function should also have a short header describing what it does. Variables, especially those declared at the class level, should have a comment explaining their function. You may know what everything is now, but you might not in 6 months. And it's easier if someone else has to look at your code (and it also improves their opinion of you as a programmer).

Now the suggestion.

You have a separate button.Click handler for each of the digits. You can simplify that by having one handler that handles all ten digits.

Private Sub DigitClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button0.Click,Button1.Click etc
    RichTextBox1.Text = RichTextBox1.Text + sender.Text
    curval = Val(RichTextBox1.Text)
    pre_curval = Val(RichTextBox1.Text)
End Sub

Just replace "etc" with the remaining buttons. You could use the same technique to group the handlers for the math function buttons as well.

And, lest I forget, congratulations on getting it working. Now make it better.

Never stop learning. What you learn today will make it easier tomorrow.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.