slight second time round code issues

HI all,

I am not very good at programming and could do with some help!
My project at the moment is to make a calculator! I have this code down for all numbers (depending on the number "1" in this case):
[blue] Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Then
TextBox1.Text = "1"
Else
Dim firstnum As Integer = TextBox1.Text
Dim finnum As Integer = firstnum & 1
TextBox1.Text = (finnum)
End If
End Sub
[/blue]
this works fine first time round but once I hit the +,-,* or / button - this code:
[blue]
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
If TextBox3.Text = "+" Then
Dim oldnum As Integer = TextBox2.Text
Dim oldnump As Integer = TextBox1.Text
Dim newnum As Integer = oldnum + oldnump
TextBox1.Text = (newnum)
TextBox2.Text = ""
TextBox3.Text = ""
End If
If TextBox3.Text = "-" Then
Dim oldnum As Integer = TextBox2.Text
Dim oldnump As Integer = TextBox1.Text
Dim newnum As Integer = oldnum - oldnump
TextBox1.Text = (newnum)
TextBox2.Text = ""
TextBox3.Text = ""
End If
If TextBox3.Text = "x" Then
Dim oldnum As Integer = TextBox2.Text
Dim oldnump As Integer = TextBox1.Text
Dim newnum As Integer = oldnum * oldnump
TextBox1.Text = (newnum)
TextBox2.Text = ""
TextBox3.Text = ""
End If
If TextBox3.Text = "/" Then
Dim oldnum As Integer = TextBox2.Text
Dim oldnump As Integer = TextBox1.Text
Dim newnum As Integer = oldnum / oldnump
TextBox1.Text = (newnum)
TextBox2.Text = ""
TextBox3.Text = ""
End If
End Sub
[/blue]
it mucks things up as soonn as I hit another number! the error that appears is:
[yellow]Conversion from string " " to type 'Integer' is not valid.[/yellow]
can anyone help me???

thanks!
Abbey

Comments

  • [b][red]This message was edited by HackmanC at 2006-5-31 13:57:7[/red][/b][hr]
    You can find the best example of a Calculator since VB 3.0 to 7.0(.NET) (Calc.vbp || Calc.sln) in the examples and the documentation MSDN that cames with it. You can learn something from there.

    Unfortunetly, the examples are not on line.

    You have the .NET code in here, but you should prefer to find one copy of MSDN:

    [code]
    Option Strict Off

    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Windows.Forms
    Imports Microsoft.VisualBasic


    Public Class Calculator
    Inherits System.Windows.Forms.Form

    'Data variables
    Private mOp1, mOp2 As Double 'Previously input operand.
    Private mNumOps As Integer 'Number of operands.
    Private mLastInput As Operation 'Indicate type of last keypress event.
    Private mOpFlag As String 'Indicate pending operation.
    Private mOpPrev As String 'Previous operation
    Private mMinus As String 'Just like a #define for "-"

    Private Enum Operation
    None = 0
    Operand
    Operator
    CE
    Cancel
    End Enum

    Public Sub New()
    MyBase.New()

    Calculator = Me

    'This call is required by the Win Form Designer.
    InitializeComponent()

    'TODO: Add any initialization after the InitializeComponent() call
    Reset()

    mMinus = "-"
    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


    #Region " Windows Form Designer generated code "
    ... :( Message text to long
    #End Region

    'BS click event
    Protected Sub CalcBS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcBS.Click
    Dim Value As String
    Value = CalcField.Text

    If ((Value.CompareTo("0.") <> 0) And (Value.Length <> 0)) Then
    Value = Value.Remove(Value.Length - 1, 1)
    End If

    If ((Value.Length = 0) Or (Value = "-")) Then
    Value = "0."
    End If

    CalcField.Text = Value
    End Sub

    'cancel click event
    Protected Sub CalcCan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcCan.Click
    Reset()
    End Sub

    'cancel entry click event
    Protected Sub CalcCE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcCE.Click
    If (mLastInput.Equals(Operation.Operand)) Then
    CalcField.Text = "0."
    ElseIf (mLastInput.Equals(Operation.Operator)) Then
    mOpFlag = mOpPrev
    End If

    mLastInput = Operation.CE
    End Sub

    Protected Sub CalcDiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcDiv.Click
    CalcRes_Click(sender, e)
    End Sub

    '+, -, *, /, = click event!
    Protected Sub CalcRes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcRes.Click
    If (CalcField.Text.Length = 0) Then
    Return
    End If

    If (mLastInput.Equals(Operation.Operand)) Then
    mNumOps = mNumOps + 1
    End If

    Select Case mNumOps
    Case 1
    mOp1 = Double.Parse(CalcField.Text)

    Case 2
    mOp2 = Double.Parse(CalcField.Text)

    Select Case mOpFlag
    Case "+"
    mOp1 = mOp1 + mOp2

    Case "-"
    mOp1 = mOp1 - mOp2

    Case "*"
    mOp1 = mOp1 * mOp2

    Case "/"
    If (mOp2 = 0) Then
    MessageBox.Show("Can't divide by zero!", "Calculator", MessageBoxButtons.OK)
    Else
    mOp1 = mOp1 / mOp2
    End If

    Case "="
    mOp1 = mOp2
    End Select

    CalcField.Text = mOp1.ToString()
    mNumOps = 1
    End Select

    mLastInput = Operation.Operator
    mOpPrev = mOpFlag

    Dim SelButton As Button
    SelButton = sender
    mOpFlag = SelButton.Text
    End Sub

    Protected Sub CalcSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcSub.Click
    CalcRes_Click(sender, e)
    End Sub

    Protected Sub CalcMul_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcMul.Click
    CalcRes_Click(sender, e)
    End Sub

    Protected Sub CalcPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcPlus.Click
    CalcRes_Click(sender, e)
    End Sub

    '+/- click event
    Protected Sub CalcSign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcSign.Click
    If (CalcField().Text.Substring(0, 1) = "-") Then
    CalcField().Text = CalcField().Text.TrimStart(mMinus.ToCharArray())
    Else
    CalcField().Text = mMinus + CalcField().Text
    End If

    mLastInput = Operation.Operand
    End Sub

    Protected Sub Calc5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc5.Click
    CalcNum_Click(sender, e)
    End Sub

    Protected Sub Calc3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc3.Click
    CalcNum_Click(sender, e)
    End Sub

    Protected Sub Calc1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc1.Click
    CalcNum_Click(sender, e)
    End Sub

    Protected Sub Calc4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc4.Click
    CalcNum_Click(sender, e)
    End Sub

    Protected Sub Calc9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc9.Click
    CalcNum_Click(sender, e)
    End Sub

    Protected Sub CalcDec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcDec.Click
    CalcNum_Click(sender, e)
    End Sub

    Protected Sub Calc8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc8.Click
    CalcNum_Click(sender, e)
    End Sub

    Protected Sub Calc2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc2.Click
    CalcNum_Click(sender, e)
    End Sub

    Protected Sub Calc0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc0.Click
    CalcNum_Click(sender, e)
    End Sub

    Protected Sub Calc7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc7.Click
    CalcNum_Click(sender, e)
    End Sub

    Protected Sub Calc6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc6.Click
    CalcNum_Click(sender, e)
    End Sub

    '0 - 9, ., click event
    Protected Sub CalcNum_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    If (mLastInput.Equals(Operation.Operand) = False) Then
    CalcField.Text = "0."
    End If

    Dim SelButton As Button
    SelButton = sender
    FormatEditField(SelButton.Text)

    mLastInput = Operation.Operand
    End Sub

    'Exit menu handler
    Protected Sub ExitMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitMenu.Click
    Application.Exit()
    End Sub

    'private helpers
    'helper to format the entry in the text box!
    Private Sub FormatEditField(ByVal NewChar As String)
    Dim Value As String
    Value = CalcField.Text

    If (Value.CompareTo("0.") = 0) Then
    Value = NewChar
    Else
    'determine if there are more than one .'s
    If (NewChar = ".") Then
    'if it's found, return
    If (Value.IndexOf(NewChar) <> -1) Then
    'don't do anything
    Return
    End If
    End If

    Value = Value + NewChar
    End If

    CalcField.Text = Value
    End Sub

    ' helper to initialize the vals
    Private Sub Reset()
    mOp1 = 0
    mOp2 = 0

    mNumOps = 0
    mLastInput = Operation.None

    mOpFlag = ""
    mOpPrev = ""

    CalcField.Text = "0."
    End Sub

    End Class
    [/code]

    [red]Good luck![/red]
    [blue]Hackman[/blue]



  • Ohhhhhh!! Damn it! I am coding in version8!!!!!!!!!!! (well that what my program tells me!)

    Any other advice???


  • [b][red]This message was edited by HackmanC at 2006-6-1 13:16:55[/red][/b][hr]
    Whow ! ... there is an 8 ? ... then you can't use the examples of 3, 4, 5, 6, 7. Damn it!

    But anyway .. your code doesnt have any syntax mistakes, and the logic appears to work, is the runtime when you have the errors.. so are the values you feed to the application..

    [code]
    If TextBox1.Text = "" Then ' If TextBox1.Text.Length() <= 0 Then
    TextBox1.Text = "1" ' Could be = " " ?
    Else
    Dim firstnum As Integer = TextBox1.Text 'This should throw the error if TextBox1.Text = " "
    Dim finnum As Integer = firstnum & 1 'WHAT DID YOU WANT TO DO WITH
    TextBox1.Text = (finnum) 'THE & .. WAS (AND) OPERATOR ?
    End If 'IN VB "&" = CONCATENATE STRINGS SO YOU GET 11.

    Conversion from string " " to type 'Integer' is not valid.
    [/code]
    ----
    : Ohhhhhh!! Damn it! I am coding in version8!!!!!!!!!!! (well that what my program tells me!)
    :
    : Any other advice???
    :
    :
    :

    [red]Good luck![/red]
    [blue]Hackman[/blue]
  • Hopefully you have figured this out by now but....

    Basically you are at some point tyring to set one of your Integer values with an non-numeric string object... I highly recommend stepping through your code to see where the error occurs and checking the values at that point. Most likely one the text values is not what you expect.

    I would also recommend something like this at the begining of each function using both values.... You should always validate input data!

    ~rlc

    [code]
    If isnumeric(text1.text) andalso isnumeric(text2.text) then
    'input values are numeric
    else
    'input values are invalid
    end if
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories