我正在使用一个简单的计算器并且它正在工作,但是一旦操作完成我就会面临清除文本框的问题而我想开始另一个。 让我们说我做了一笔钱,结果是6,当我点击一个数字开始另一个操作时,它没有清除文本框,我想知道我该怎么办?
因为我是初学者,请保持简单的答案。
这是我目前的代码:
Public Class Form1
Dim FirstNumber As Long
Dim Operation As String
'BUTTONS
'Numbers
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Viewer.Text = Viewer.Text + "1"
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Viewer.Text = Viewer.Text + "2"
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Viewer.Text = Viewer.Text + "3"
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Viewer.Text = Viewer.Text + "4"
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Viewer.Text = Viewer.Text + "5"
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Viewer.Text = Viewer.Text + "6"
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Viewer.Text = Viewer.Text + "7"
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
Viewer.Text = Viewer.Text + "8"
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
Viewer.Text = Viewer.Text + "9"
End Sub
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Zero.Click
Viewer.Text = Viewer.Text + "0"
End Sub
'Buttons "," "del" "clear"
Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Dot.Click
Viewer.Text = Viewer.Text + ","
End Sub
Private Sub Button17_Click(sender As Object, e As EventArgs) Handles Del.Click
Viewer.Text = Viewer.Text.Remove(Viewer.Text.Length - 1, 1)
End Sub
Private Sub Clear_Click(sender As Object, e As EventArgs) Handles Clear.Click
Viewer.Text = ""
End Sub
Private Sub Sum_Click(sender As Object, e As EventArgs) Handles Sum.Click
FirstNumber = Val(Viewer.Text)
Operation = "SUM"
Viewer.Text = ""
End Sub
Private Sub Minus_Click(sender As Object, e As EventArgs) Handles Minus.Click
FirstNumber = Val(Viewer.Text)
Operation = "MINUS"
Viewer.Text = ""
End Sub
Private Sub Multi_Click(sender As Object, e As EventArgs) Handles Multi.Click
FirstNumber = Val(Viewer.Text)
Operation = "MULTI"
Viewer.Text = ""
End Sub
Private Sub Div_Click(sender As Object, e As EventArgs) Handles Div.Click
FirstNumber = Val(Viewer.Text)
Operation = "DIV"
Viewer.Text = ""
End Sub
Private Sub Result_Click(sender As Object, e As EventArgs) Handles Result.Click
Dim SecondNumber As Long
Dim Result As Double
SecondNumber = Val(Viewer.Text)
'Fx
If Operation = "SUM" Then
Result = FirstNumber + SecondNumber
End If
If Operation = "MINUS" Then
Result = FirstNumber - SecondNumber
End If
If Operation = "MULTI" Then
Result = FirstNumber * SecondNumber
End If
If Operation = "DIV" Then
Result = FirstNumber / SecondNumber
End If
Viewer.Text = Result
End Sub
End Class
答案 0 :(得分:0)
尝试使用boolean类型的simpel全局变量,表示要在添加数字之前清除文本,如下所示:
Public Class Form1
Dim FirstNumber As Long
Dim Operation As String
Dim ClearField as Boolean = True 'At program start, you do want to start over at first
...
然后你应该使用一个新的子句向观众添加文字,使代码更清晰,避免重复:
...
Private Sub AddNumber(NewValue as String)
If ClearField = True Then Viewer.Text = ""
Viewer.Text &= NewValue
ClearField = False
End Sub
...
清除字段一次后,将ClearField
变量设置为false,以避免再次清除该字段。
在Button.Click
事件处理程序中,您只需使用此子而不是手动添加文本:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
AddNumber("1")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
AddNumber("2")
End Sub
等等。 计算结果后,您可以将ClearField变量设置为true,以便程序在下次添加数字时启动:
...
Viewer.Text = Result
ClearField = True
...
答案 1 :(得分:0)
在代码的开头取一个标志变量。在结果单击事件上,设置标志。下次单击数字按钮时,请检查标志。如果设置:
在结果点击事件中,在Viewer.Text = Result
flag=1
以下是按钮的示例:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If flag=1 then
Viewer.Text = ""
Viewer.Text = Viewer.Text + "1"
flag = 0
Else
Viewer.Text = Viewer.Text + "1"
End If
End Sub
为每个按钮执行此操作。
答案 2 :(得分:0)
使用全局变量来识别键入是数字和操作的顺序,还是在结果按钮之后是否是这样的序列的开始。
Public Class Form1
Dim FirstNumber As Long
Dim Operation As String
Dim ResultPressed As Boolean
'BUTTONS
'Numbers
Private Sub Typing()
IF ResultPressed Then ' Result button was already pressed. This number or operation typing is first button press after result button
Viewer.Text = ""; ' so let's clear textbox
ResultPressed = False;
End If
' If ResultPressed is false, nothing to do. Continue typing numbers ..
End
Private Sub EndTyping()
ResultPressed = True;
End
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Typing number or some operation fce Typing() will decides if clear textbox..
Typing()
Viewer.Text = Viewer.Text + "1"
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' Typing number or some operation fce Typing() will decides if clear textbox..
Typing()
Viewer.Text = Viewer.Text + "2"
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
' Typing number or some operation fce Typing() will decides if clear textbox..
Typing()
Viewer.Text = Viewer.Text + "3"
End Sub
Private Sub Result_Click(sender As Object, e As EventArgs) Handles Result.Click
Dim SecondNumber As Long
Dim Result As Double
' Numbers and operation typing finished.
EndTyping()
SecondNumber = Val(Viewer.Text)