我的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
答案 0 :(得分:1)
如果我改变你的选择案例,似乎对我来说工作正常:
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