problem with VB code

problem with variables

Page 1 of 1

2 Replies - 785 Views - Last Post: 10 April 2008 - 07:57 PM Rate Topic: -----

#1 pro-grammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 10-April 08

problem with VB code

Posted 10 April 2008 - 08:43 AM

hi, i am new to this site so please be gentle. The problem i have is described below, and i wrote the code in VisualStudio 2005

I am trying to make a calculator. The problem i have is that the value in the 1st textbox assigns to the first variable (used for 1st number), the value in the 2nd textbox assigns to the sign variable (to store the operator, - + / *) but then for some reason i cannot add anything into the 3rd textbox which should assign the 2nd variable (used for 2nd number)..... i will post all the code below including a screenshot of the form for you to see.

please help me because i have tried to work this out for a few days and i cant see anything wrong with the code but for some reason it isnt working properly (like i said, it wont add anything to the 2nd variable)

form:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.txtDisplay = New System.Windows.Forms.TextBox
        Me.btn0 = New System.Windows.Forms.Button
        Me.btn2 = New System.Windows.Forms.Button
        Me.btn3 = New System.Windows.Forms.Button
        Me.btn1 = 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.btn9 = New System.Windows.Forms.Button
        Me.btn8 = New System.Windows.Forms.Button
        Me.btn7 = New System.Windows.Forms.Button
        Me.btnDot = New System.Windows.Forms.Button
        Me.btnEquals = New System.Windows.Forms.Button
        Me.btnPlus = New System.Windows.Forms.Button
        Me.btnMinus = New System.Windows.Forms.Button
        Me.btnMultiply = New System.Windows.Forms.Button
        Me.btnDivide = New System.Windows.Forms.Button
        Me.btnMAdd = New System.Windows.Forms.Button
        Me.btnMErase = New System.Windows.Forms.Button
        Me.btnMRecall = New System.Windows.Forms.Button
        Me.btnSquareRt = New System.Windows.Forms.Button
        Me.btnPercent = New System.Windows.Forms.Button
        Me.btnCE = New System.Windows.Forms.Button
        Me.btnNegative = New System.Windows.Forms.Button
        Me.txtSign = New System.Windows.Forms.TextBox
        Me.txtAnswer = New System.Windows.Forms.TextBox
        Me.txtDisplay2 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'txtDisplay
        '
        Me.txtDisplay.Location = New System.Drawing.Point(36, 117)
        Me.txtDisplay.Name = "txtDisplay"
        Me.txtDisplay.ReadOnly = True
        Me.txtDisplay.Size = New System.Drawing.Size(372, 20)
        Me.txtDisplay.TabIndex = 0
        '
        'btn0
        '
        Me.btn0.Location = New System.Drawing.Point(120, 350)
        Me.btn0.Name = "btn0"
        Me.btn0.Size = New System.Drawing.Size(36, 29)
        Me.btn0.TabIndex = 0
        Me.btn0.Text = "0"
        Me.btn0.UseVisualStyleBackColor = True
        '
        'btn2
        '
        Me.btn2.Location = New System.Drawing.Point(162, 315)
        Me.btn2.Name = "btn2"
        Me.btn2.Size = New System.Drawing.Size(36, 29)
        Me.btn2.TabIndex = 2
        Me.btn2.Text = "2"
        Me.btn2.UseVisualStyleBackColor = True
        '
        'btn3
        '
        Me.btn3.Location = New System.Drawing.Point(204, 315)
        Me.btn3.Name = "btn3"
        Me.btn3.Size = New System.Drawing.Size(36, 29)
        Me.btn3.TabIndex = 3
        Me.btn3.Text = "3"
        Me.btn3.UseVisualStyleBackColor = True
        '
        'btn1
        '
        Me.btn1.Location = New System.Drawing.Point(120, 315)
        Me.btn1.Name = "btn1"
        Me.btn1.Size = New System.Drawing.Size(36, 29)
        Me.btn1.TabIndex = 1
        Me.btn1.Text = "1"
        Me.btn1.UseVisualStyleBackColor = True
        '
        'btn4
        '
        Me.btn4.Location = New System.Drawing.Point(120, 280)
        Me.btn4.Name = "btn4"
        Me.btn4.Size = New System.Drawing.Size(36, 29)
        Me.btn4.TabIndex = 4
        Me.btn4.Text = "4"
        Me.btn4.UseVisualStyleBackColor = True
        '
        'btn5
        '
        Me.btn5.Location = New System.Drawing.Point(162, 280)
        Me.btn5.Name = "btn5"
        Me.btn5.Size = New System.Drawing.Size(36, 29)
        Me.btn5.TabIndex = 5
        Me.btn5.Text = "5"
        Me.btn5.UseVisualStyleBackColor = True
        '
        'btn6
        '
        Me.btn6.Location = New System.Drawing.Point(204, 280)
        Me.btn6.Name = "btn6"
        Me.btn6.Size = New System.Drawing.Size(36, 29)
        Me.btn6.TabIndex = 6
        Me.btn6.Text = "6"
        Me.btn6.UseVisualStyleBackColor = True
        '
        'btn9
        '
        Me.btn9.Location = New System.Drawing.Point(204, 245)
        Me.btn9.Name = "btn9"
        Me.btn9.Size = New System.Drawing.Size(36, 29)
        Me.btn9.TabIndex = 9
        Me.btn9.Text = "9"
        Me.btn9.UseVisualStyleBackColor = True
        '
        'btn8
        '
        Me.btn8.Location = New System.Drawing.Point(162, 245)
        Me.btn8.Name = "btn8"
        Me.btn8.Size = New System.Drawing.Size(36, 29)
        Me.btn8.TabIndex = 8
        Me.btn8.Text = "8"
        Me.btn8.UseVisualStyleBackColor = True
        '
        'btn7
        '
        Me.btn7.Location = New System.Drawing.Point(120, 245)
        Me.btn7.Name = "btn7"
        Me.btn7.Size = New System.Drawing.Size(36, 29)
        Me.btn7.TabIndex = 7
        Me.btn7.Text = "7"
        Me.btn7.UseVisualStyleBackColor = True
        '
        'btnDot
        '
        Me.btnDot.Location = New System.Drawing.Point(162, 350)
        Me.btnDot.Name = "btnDot"
        Me.btnDot.Size = New System.Drawing.Size(36, 29)
        Me.btnDot.TabIndex = 10
        Me.btnDot.Text = "."
        Me.btnDot.UseVisualStyleBackColor = True
        '
        'btnEquals
        '
        Me.btnEquals.Location = New System.Drawing.Point(204, 350)
        Me.btnEquals.Name = "btnEquals"
        Me.btnEquals.Size = New System.Drawing.Size(36, 29)
        Me.btnEquals.TabIndex = 11
        Me.btnEquals.Text = "="
        Me.btnEquals.UseVisualStyleBackColor = True
        '
        'btnPlus
        '
        Me.btnPlus.Location = New System.Drawing.Point(284, 315)
        Me.btnPlus.Name = "btnPlus"
        Me.btnPlus.Size = New System.Drawing.Size(36, 64)
        Me.btnPlus.TabIndex = 12
        Me.btnPlus.Text = "+"
        Me.btnPlus.UseVisualStyleBackColor = True
        '
        'btnMinus
        '
        Me.btnMinus.Location = New System.Drawing.Point(284, 280)
        Me.btnMinus.Name = "btnMinus"
        Me.btnMinus.Size = New System.Drawing.Size(36, 29)
        Me.btnMinus.TabIndex = 13
        Me.btnMinus.Text = "-"
        Me.btnMinus.UseVisualStyleBackColor = True
        '
        'btnMultiply
        '
        Me.btnMultiply.Location = New System.Drawing.Point(284, 245)
        Me.btnMultiply.Name = "btnMultiply"
        Me.btnMultiply.Size = New System.Drawing.Size(36, 29)
        Me.btnMultiply.TabIndex = 14
        Me.btnMultiply.Text = "*"
        Me.btnMultiply.UseVisualStyleBackColor = True
        '
        'btnDivide
        '
        Me.btnDivide.Location = New System.Drawing.Point(284, 210)
        Me.btnDivide.Name = "btnDivide"
        Me.btnDivide.Size = New System.Drawing.Size(36, 29)
        Me.btnDivide.TabIndex = 15
        Me.btnDivide.Text = "/"
        Me.btnDivide.UseVisualStyleBackColor = True
        '
        'btnMAdd
        '
        Me.btnMAdd.Location = New System.Drawing.Point(204, 210)
        Me.btnMAdd.Name = "btnMAdd"
        Me.btnMAdd.Size = New System.Drawing.Size(36, 29)
        Me.btnMAdd.TabIndex = 16
        Me.btnMAdd.Text = "M+"
        Me.btnMAdd.UseVisualStyleBackColor = True
        '
        'btnMErase
        '
        Me.btnMErase.Location = New System.Drawing.Point(162, 210)
        Me.btnMErase.Name = "btnMErase"
        Me.btnMErase.Size = New System.Drawing.Size(36, 29)
        Me.btnMErase.TabIndex = 17
        Me.btnMErase.Text = "M-"
        Me.btnMErase.UseVisualStyleBackColor = True
        '
        'btnMRecall
        '
        Me.btnMRecall.Location = New System.Drawing.Point(109, 210)
        Me.btnMRecall.Name = "btnMRecall"
        Me.btnMRecall.Size = New System.Drawing.Size(47, 29)
        Me.btnMRecall.TabIndex = 18
        Me.btnMRecall.Text = "MRC"
        Me.btnMRecall.UseVisualStyleBackColor = True
        '
        'btnSquareRt
        '
        Me.btnSquareRt.Location = New System.Drawing.Point(204, 175)
        Me.btnSquareRt.Name = "btnSquareRt"
        Me.btnSquareRt.Size = New System.Drawing.Size(36, 29)
        Me.btnSquareRt.TabIndex = 21
        Me.btnSquareRt.Text = "√"
        Me.btnSquareRt.UseVisualStyleBackColor = True
        '
        'btnPercent
        '
        Me.btnPercent.Location = New System.Drawing.Point(162, 175)
        Me.btnPercent.Name = "btnPercent"
        Me.btnPercent.Size = New System.Drawing.Size(36, 29)
        Me.btnPercent.TabIndex = 20
        Me.btnPercent.Text = "%"
        Me.btnPercent.UseVisualStyleBackColor = True
        '
        'btnCE
        '
        Me.btnCE.Location = New System.Drawing.Point(109, 175)
        Me.btnCE.Name = "btnCE"
        Me.btnCE.Size = New System.Drawing.Size(47, 29)
        Me.btnCE.TabIndex = 19
        Me.btnCE.Text = "CE"
        Me.btnCE.UseVisualStyleBackColor = True
        '
        'btnNegative
        '
        Me.btnNegative.Location = New System.Drawing.Point(284, 175)
        Me.btnNegative.Name = "btnNegative"
        Me.btnNegative.Size = New System.Drawing.Size(36, 29)
        Me.btnNegative.TabIndex = 22
        Me.btnNegative.Text = "+/-"
        Me.btnNegative.UseVisualStyleBackColor = True
        '
        'txtSign
        '
        Me.txtSign.Location = New System.Drawing.Point(36, 91)
        Me.txtSign.Name = "txtSign"
        Me.txtSign.ReadOnly = True
        Me.txtSign.Size = New System.Drawing.Size(372, 20)
        Me.txtSign.TabIndex = 23
        '
        'txtAnswer
        '
        Me.txtAnswer.Location = New System.Drawing.Point(36, 12)
        Me.txtAnswer.Name = "txtAnswer"
        Me.txtAnswer.ReadOnly = True
        Me.txtAnswer.Size = New System.Drawing.Size(372, 20)
        Me.txtAnswer.TabIndex = 25
        '
        'txtDisplay2
        '
        Me.txtDisplay2.Location = New System.Drawing.Point(36, 65)
        Me.txtDisplay2.Name = "txtDisplay2"
        Me.txtDisplay2.ReadOnly = True
        Me.txtDisplay2.Size = New System.Drawing.Size(372, 20)
        Me.txtDisplay2.TabIndex = 26
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(436, 405)
        Me.Controls.Add(Me.txtDisplay2)
        Me.Controls.Add(Me.txtAnswer)
        Me.Controls.Add(Me.txtSign)
        Me.Controls.Add(Me.btnNegative)
        Me.Controls.Add(Me.btnSquareRt)
        Me.Controls.Add(Me.btnPercent)
        Me.Controls.Add(Me.btnCE)
        Me.Controls.Add(Me.btnMRecall)
        Me.Controls.Add(Me.btnMErase)
        Me.Controls.Add(Me.btnMAdd)
        Me.Controls.Add(Me.btnDivide)
        Me.Controls.Add(Me.btnMultiply)
        Me.Controls.Add(Me.btnMinus)
        Me.Controls.Add(Me.btnPlus)
        Me.Controls.Add(Me.btnEquals)
        Me.Controls.Add(Me.btnDot)
        Me.Controls.Add(Me.btn9)
        Me.Controls.Add(Me.btn8)
        Me.Controls.Add(Me.btn7)
        Me.Controls.Add(Me.btn6)
        Me.Controls.Add(Me.btn5)
        Me.Controls.Add(Me.btn4)
        Me.Controls.Add(Me.btn1)
        Me.Controls.Add(Me.btn3)
        Me.Controls.Add(Me.btn2)
        Me.Controls.Add(Me.btn0)
        Me.Controls.Add(Me.txtDisplay)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents txtDisplay As System.Windows.Forms.TextBox
    Friend WithEvents btn0 As System.Windows.Forms.Button
    Friend WithEvents btn2 As System.Windows.Forms.Button
    Friend WithEvents btn3 As System.Windows.Forms.Button
    Friend WithEvents btn1 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 btn9 As System.Windows.Forms.Button
    Friend WithEvents btn8 As System.Windows.Forms.Button
    Friend WithEvents btn7 As System.Windows.Forms.Button
    Friend WithEvents btnDot As System.Windows.Forms.Button
    Friend WithEvents btnEquals As System.Windows.Forms.Button
    Friend WithEvents btnPlus As System.Windows.Forms.Button
    Friend WithEvents btnMinus As System.Windows.Forms.Button
    Friend WithEvents btnMultiply As System.Windows.Forms.Button
    Friend WithEvents btnDivide As System.Windows.Forms.Button
    Friend WithEvents btnMAdd As System.Windows.Forms.Button
    Friend WithEvents btnMErase As System.Windows.Forms.Button
    Friend WithEvents btnMRecall As System.Windows.Forms.Button
    Friend WithEvents btnSquareRt As System.Windows.Forms.Button
    Friend WithEvents btnPercent As System.Windows.Forms.Button
    Friend WithEvents btnCE As System.Windows.Forms.Button
    Friend WithEvents btnNegative As System.Windows.Forms.Button
    Friend WithEvents txtSign As System.Windows.Forms.TextBox
    Friend WithEvents txtAnswer As System.Windows.Forms.TextBox
    Friend WithEvents txtDisplay2 As System.Windows.Forms.TextBox
End Class
Code:
Public Class Form1

    'Inherits System.Windows.Forms.Form

    Dim Numbers() As Integer = New Integer(1) {total1, total2}
    Dim total1 As Integer
    Dim total2 As Integer
    Dim sign As String
    Dim total3 As Decimal
    Dim read As Double

    Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click

        If txtDisplay.Text = "" Then
            txtDisplay.Text = ""
        Else
            txtDisplay.Text = txtDisplay.Text & "0"
        End If

    End Sub

    Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click

        txtDisplay.Text = txtDisplay.Text & "1"

    End Sub

    Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click

        txtDisplay.Text = txtDisplay.Text & "2"

    End Sub

    Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click

        txtDisplay.Text = txtDisplay.Text & "3"

    End Sub

    Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click

        txtDisplay.Text = txtDisplay.Text & "4"

    End Sub

    Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click

        txtDisplay.Text = txtDisplay.Text & "5"

    End Sub

    Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click

        txtDisplay.Text = txtDisplay.Text & "6"

    End Sub

    Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click

        txtDisplay.Text = txtDisplay.Text & "7"

    End Sub

    Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click

        txtDisplay.Text = txtDisplay.Text & "8"

    End Sub

    Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click

        txtDisplay.Text = txtDisplay.Text & "9"

    End Sub

    Private Sub btnDot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDot.Click

        If txtDisplay.Text = "." Then
            txtDisplay.Text = total3
        Else
            txtDisplay.Text = txtDisplay.Text & "."
        End If

        btnDot.Enabled = False

    End Sub

    Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click

        total1 = txtDisplay.Text
        sign = "+"
        txtSign.Text = sign
        MsgBox(total1)
        MsgBox(total2)


    End Sub

    Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click

        total1 = txtDisplay.Text
        sign = "-"
        txtSign.Text = sign
        MsgBox(total1)
        MsgBox(total2)

    End Sub

    Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click

        total1 = txtDisplay.Text
        sign = "*"
        txtSign.Text = sign
        MsgBox(total1)
        MsgBox(total2)

    End Sub

    Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click

        total1 = txtDisplay.Text
        sign = "/"
        txtSign.Text = sign
        MsgBox(total1)
        MsgBox(total2)

    End Sub

    Private Sub btnNegative_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNegative.Click

        total1 = txtDisplay.Text
        sign = "-"
        txtSign.Text = sign
        MsgBox(total1)
        MsgBox(total2)

    End Sub

End Class


This post has been edited by PsychoCoder: 10 April 2008 - 04:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: problem with VB code

#2 ferrari12508  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3
  • View blog
  • Posts: 1,115
  • Joined: 02-November 07

Re: problem with VB code

Posted 10 April 2008 - 04:09 PM

Your code will not add anything into the second textbox, because it just replaces what is in the first textbox. I would suggest instead of having buttons, just letting the user enter the number.There is a tutorial on here about how to make a calculator. something you could do is add the code
If not sign = "" then
txtDisplay2.Text = txtDisplay2.Text & "0"
end if


You could use that so when you click to enter numbers after you have clicked a symbol for addition or subtraction or whatever is entered, it would add the numbers into the final textbox.
Was This Post Helpful? 0
  • +
  • -

#3 JasonMcAuley  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 144
  • Joined: 10-April 08

Re: problem with VB code

Posted 10 April 2008 - 07:57 PM

As ferrari said; it doesn't seem like you are making reference to the 2nd textbox; so it doesn't know to put it in there.

As he suggested; the ideal time to switch entry from the first number text box to the second number text box would be when the sign is identified. You can either do it by checking the value in the sign box; or simply setting a flag when sign is filled.

Either way works; its up to you!

Good luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1