我想做的是普通人如何添加数字,即他们将按下数字按钮并按此顺序点击加号按钮
2 + 5 + 6 + 8(计算器不断添加数字并显示添加结果)。在vb.net
答案 0 :(得分:0)
我会帮助您使用以下代码:
以下是表单的外观:
现在:添加5个按钮:" 2" {Num2BUT}," 5" {Num5BUT}," 6" {Num6BUT}," 8" {Num8BUT}," +" {PlusBUT}
和2个TexBox:{ANTextBox},{ResultTextBox}。
以下是代码:
Public Class Form1
Dim X1, X2 As Integer
Private Sub Num2BUT_Click(sender As Object, e As EventArgs) Handles Num2BUT.Click
ANTextBox.Text = ANTextBox.Text + "2 "
If X1 = 0 Then
X1 = 2
ElseIf X2 = 0 Then
X2 = 2
Else
X2 = 2
End If
End Sub
Private Sub Num5BUT_Click(sender As Object, e As EventArgs) Handles Num5BUT.Click
ANTextBox.Text = ANTextBox.Text + "5 "
If X1 = 0 Then
X1 = 5
ElseIf X2 = 0 Then
X2 = 5
Else
X2 = 5
End If
End Sub
Private Sub Num6BUT_Click(sender As Object, e As EventArgs) Handles Num6BUT.Click
ANTextBox.Text = ANTextBox.Text + "6 "
If X1 = 0 Then
X1 = 6
ElseIf X2 = 0 Then
X2 = 6
Else
X2 = 6
End If
End Sub
Private Sub Num8BUT_Click(sender As Object, e As EventArgs) Handles Num8BUT.Click
ANTextBox.Text = ANTextBox.Text + "8 "
If X1 = 0 Then
X1 = 8
ElseIf X2 = 0 Then
X2 = 8
Else
X2 = 8
End If
End Sub
Private Sub PlusBUT_Click(sender As Object, e As EventArgs) Handles PlusBUT.Click
If Not ANTextBox.Text = "" Then
If ResultTextBox.Text = "" Then
ANTextBox.Text = ANTextBox.Text + "+ "
If Not X1 = 0 And Not X2 = 0 Then
ResultTextBox.Text = X1 + X2
End If
Else
ANTextBox.Text = ANTextBox.Text + "+ "
ResultTextBox.Text = ResultTextBox.Text + X2
End If
End If
End Sub
结束班
注意:
此代码仅适用于4个数字,因此您只需添加更多按钮并复制其中一个按钮的代码并更改数字(X1 =" 1",X1 =" 9& #34; .... ECT)
答案 1 :(得分:0)
我理解它的方式是你想拥有两个文本框。其中一个将显示结果,第二个用于键入数字,然后是加号。
这是一个非常简单的解决方案:
我们假设TextBox2是处理数字条目的文本框:
Dim NumberToHold As Double = 0
Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged
If TextBox2.Text.Contains("+") Then
TextBox2.Text = TextBox2.Text.Replace("+", "")
TextBox2.Text = TextBox2.Text.Replace(" ", "")
NumberToHold = Val(TextBox2.Text) + NumberToHold
TextBox1.Text = NumberToHold
TextBox2.Clear()
End If
End Sub
答案 2 :(得分:0)
好的,这是表单的样子:
现在,就像第一个;添加5个按钮:" 2" {Num2BUT}," 5" {Num5BUT}," 6" {Num6BUT}," 8" {Num8BUT}," +" {PlusBUT}
但现在为结果添加一个TexBox:{ANTResultTextBox}和隐藏 textBox {HiddenTextBox}
以下是代码:
Public Class Form1
Dim X1, X2 As Integer
Private Sub Num2BUT_Click(sender As Object, e As EventArgs) Handles Num2BUT.Click
If HiddenTextBox.Text = "Plus" Then
X2 = 2
ANTextBox.Text = ANTextBox.Text + X2
HiddenTextBox.Text = ""
Else
ANTextBox.Text = ANTextBox.Text & "2"
X1 = 2
End If
End Sub
Private Sub Num5BUT_Click(sender As Object, e As EventArgs) Handles Num5BUT.Click
If HiddenTextBox.Text = "Plus" Then
X2 = 5
ANTextBox.Text = ANTextBox.Text + X2
HiddenTextBox.Text = ""
Else
ANTextBox.Text = ANTextBox.Text & "5"
X1 = 5
End If
End Sub
Private Sub Num6BUT_Click(sender As Object, e As EventArgs) Handles Num6BUT.Click
If HiddenTextBox.Text = "Plus" Then
X2 = 6
ANTextBox.Text = ANTextBox.Text + X2
HiddenTextBox.Text = ""
Else
ANTextBox.Text = ANTextBox.Text & "6"
X1 = 6
End If
End Sub
Private Sub Num8BUT_Click(sender As Object, e As EventArgs) Handles Num8BUT.Click
If HiddenTextBox.Text = "Plus" Then
X2 = 8
ANTextBox.Text = ANTextBox.Text + X2
HiddenTextBox.Text = ""
Else
ANTextBox.Text = ANTextBox.Text & "8"
X1 = 8
End If
End Sub
Private Sub PlusBUT_Click(sender As Object, e As EventArgs) Handles PlusBUT.Click
ANTextBox.Text = ANTextBox.Text + " + "
HiddenTextBox.Text = "Plus"
End Sub
结束班
答案 3 :(得分:0)
这里是代码,该表单基本上被设计为文本框和用于加,减,乘和除的按钮。该代码可以用一位数字对数字进行加,减,乘和除,但您可以这样做。
Public Class FrmCalculator
Dim PreviousNumber, NextNumber, DecPlace, Dec As Decimal
Dim CurrentNumber, State As String
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
CurrentNumber = 1
txtNumber.Text = "1"
End Sub
Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
CurrentNumber = 2
txtNumber.Text = "2"
End Sub
Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
CurrentNumber = 3
txtNumber.Text = "3"
End Sub
Private Sub btn4_Click(sender As Object, e As EventArgs) Handles btn4.Click
CurrentNumber = 4
txtNumber.Text = "4"
End Sub
Private Sub btn5_Click(sender As Object, e As EventArgs) Handles btn5.Click
CurrentNumber = 5
txtNumber.Text = "5"
End Sub
Private Sub btn6_Click(sender As Object, e As EventArgs) Handles btn6.Click
CurrentNumber = 6
txtNumber.Text = "6"
End Sub
Private Sub btn7_Click(sender As Object, e As EventArgs) Handles btn7.Click
CurrentNumber = 7
txtNumber.Text = "7"
End Sub
Private Sub btn8_Click(sender As Object, e As EventArgs) Handles btn8.Click
CurrentNumber = 8
txtNumber.Text = "8"
End Sub
Private Sub btn9_Click(sender As Object, e As EventArgs) Handles btn9.Click
CurrentNumber = 9
txtNumber.Text = "9"
End Sub
Private Sub btnEqual_Click(sender As Object, e As EventArgs) Handles btnEqual.Click
If State = "M" Then
NextNumber = PreviousNumber * CurrentNumber
ElseIf State = "A" Then
NextNumber = PreviousNumber + CurrentNumber
ElseIf State = "S" Then
NextNumber = PreviousNumber - CurrentNumber
ElseIf State = "D" Then
NextNumber = PreviousNumber / CurrentNumber
txtNumber.Text = NextNumber
End Sub
Private Sub btnSubtract_Click(sender As Object, e As EventArgs) Handles btnSubtract.Click
PreviousNumber = CurrentNumber
txtNumber.Clear()
State = "S"
End Sub
Private Sub btnDivide_Click(sender As Object, e As EventArgs) Handles btnDivide.Click
PreviousNumber = CurrentNumber
txtNumber.Clear()
State = "D"
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
PreviousNumber = CurrentNumber
txtNumber.Clear()
State = "A"
End Sub
Private Sub btnMultiply_Click(sender As Object, e As EventArgs) Handles btnMultiply.Click
PreviousNumber = CurrentNumber
txtNumber.Clear()
State = "M"
'NextNumber = PreviousNumber * CurrentNumber
End Sub