我最后有一个带有此代码的计算器:
'按下超过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!")
TextBox1.Text = TextBox1.Text.Remove(TextBox1.Text.Length - 1, 1)
但是当我对计算器进行总结时,文本框仍然限于数字,因此所有答案都是3位数。
如何更改代码,以便在输入数字时限制文本框,但在回答总和时却不会?
答案 0 :(得分:0)
您可以使用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
答案 1 :(得分:0)
在表单级别获取布尔变量“IsModifiedByUser”。默认情况下,它应该是true。当您在程序上修改文本框时,在此之前将IsModifiedByUser的值设置为false。在您以编程方式修改文本框之后,再次将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