базовый калькулятор asp.net - PullRequest
       2

базовый калькулятор asp.net

0 голосов
/ 03 декабря 2011

У меня возникла проблема в моем проекте калькулятора ASP .NET. Я использую VB10, все коды работают, кроме кнопки равенства. Когда мой ввод 1 + 1, результат не будет отображаться в текстовом поле. Я пробовал другое решение, но все равно не отображает результат. Заранее спасибо.

Вот мой код:

Partial Class webcalc
    Inherits System.Web.UI.Page
    Dim total As Double
    Dim total1 As Double
    Dim myOP As String
    Dim sign As Integer
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btn0_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn0.Click
        txtOutput.Text = txtOutput.Text + btn0.Text
    End Sub

    Protected Sub btn1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn1.Click
        txtOutput.Text = txtOutput.Text + btn1.Text
    End Sub

    Protected Sub btn2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn2.Click
        txtOutput.Text = txtOutput.Text + btn2.Text
    End Sub

    Protected Sub btn3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn3.Click
        txtOutput.Text = txtOutput.Text + btn3.Text
    End Sub

    Protected Sub btn4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn4.Click
        txtOutput.Text = txtOutput.Text + btn4.Text
    End Sub

    Protected Sub btn5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn5.Click
        txtOutput.Text = txtOutput.Text + btn5.Text
    End Sub

    Protected Sub btn6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn6.Click
        txtOutput.Text = txtOutput.Text + btn6.Text
    End Sub

    Protected Sub btn7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn7.Click
        txtOutput.Text = txtOutput.Text + btn7.Text
    End Sub

    Protected Sub btn8_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn8.Click
        txtOutput.Text = txtOutput.Text + btn8.Text
    End Sub

    Protected Sub btn9_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn9.Click
        txtOutput.Text = txtOutput.Text + btn9.Text
    End Sub

    Protected Sub btndot_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btndot.Click
        txtOutput.Text = txtOutput.Text + btndot.Text
    End Sub

    '============================================OPERATIONS============================================================

    Protected Sub btnplus_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnplus.Click
        total = Double.Parse(txtOutput.Text)
        txtOutput.Text = " "
        myOP = "+"
        sign = 1

    End Sub

    Protected Sub btnmin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnmin.Click
        total = Double.Parse(txtOutput.Text)
        txtOutput.Text = " "
        myOP = "-"
        sign = 2

    End Sub

    Protected Sub btnmul_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnmul.Click
        total = Double.Parse(txtOutput.Text)
        txtOutput.Text = " "
        myOP = "*"
        sign = 3

    End Sub

    Protected Sub btndiv_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btndiv.Click
        total = Double.Parse(txtOutput.Text)
        txtOutput.Text = " "
        myOP = "/"
        sign = 4
    End Sub

    Protected Sub btndel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btndel.Click
        txtOutput.Text = " "
    End Sub

    Protected Sub btneq_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btneq.Click
        Call eq()

    End Sub

    Public Sub eq()
        Select Case myOP

            Case Is = "+"
                total1 = total + Double.Parse(txtOutput.Text)
                'txtOutput.Text = total1.ToString()
                total = 0

            Case Is = "-"
                total1 = total - Double.Parse(txtOutput.Text)
                txtOutput.Text = total1.ToString()
                total = 0

            Case Is = "*"
                total1 = total * Double.Parse(txtOutput.Text)
                txtOutput.Text = total1.ToString()
                total = 0

            Case Is = "/"
                total1 = total / Double.Parse(txtOutput.Text)
                txtOutput.Text = total1.ToString()
                total = 0


        End Select

    End Sub
End Class

1 Ответ

1 голос
/ 03 декабря 2011

Мне кажется, что все работает нормально, если я изменю ваш выбранный случай следующим образом:

myOP = CType(ViewState("myOP"), String)
total = CType(ViewState("total"), Double)

Select Case myOP
        Case "+"
            total1 = total + Double.Parse(txtOutput.Text)
            'txtOutput.Text = total1.ToString()
            total = 0

        Case "-"
            total1 = total - Double.Parse(txtOutput.Text)
            txtOutput.Text = total1.ToString()
            total = 0

        .....
        .....    
End Select

и изменим событие нажатия кнопки оператора следующим образом:

Protected Sub btnplus_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnplus.Click        
        total = Double.Parse(txtOutput.Text)
        ViewState("total") = total
        txtOutput.Text = " "
        myOP = "+"
        ViewState("myOP") = myOP
        sign = 1

End Sub
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...