Vb.net 高级计算器
我有一个计算器,末尾有以下代码: '当按下超过3位数字时的代码'Vb.net 高级计算器,vb.net,calculator,Vb.net,Calculator,我有一个计算器,末尾有以下代码: '当按下超过3位数字时的代码' Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged If TextBox1.Text.Length > 3 Then MsgBox("You can't add any more numbers!")
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Length > 3 Then
MsgBox("You can't add any more numbers!")
TextBox1.Text = TextBox1.Text.Remove(TextBox1.Text.Length - 1, 1)
但是当我在计算器上求和时,文本框仍然限于数字,所以所有的答案都是3位数
如何更改代码,使其在输入数字时限制文本框,而在回答总和时不限制文本框?您可以使用e.Cancel=True
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Length > 3 Then
MsgBox("You can't add any more numbers!")
e.Cancel = True
在表单级别使用布尔变量“IsModifiedByUser”。默认情况下,它应该为true。当以编程方式修改文本框时,在此之前将IsModifiedByUser的值设置为false。通过编程修改textbox后,再次将IsModifiedByUser的值设置为true。仅当IsModifiedByUser为true时,才检查文本框值的长度
Private IsModifiedByUser As Boolean = True
Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
If TextBox1.Text.Length > 3 AndAlso IsModifiedByUser Then
MsgBox("You can't add any more numbers!")
e.Cancel = True
End If
End Sub
Public Sub CalculateValue()
IsModifiedByUser = False
'Do the calculation and set the value in textbox
IsModifiedByUser = True
End Sub
我是一个非常非常业余的程序员,我不知道你在说什么=