【发布时间】:2020-12-27 17:05:32
【问题描述】:
基本上我想在文本框中输入一个数字。如果数字大于 80,则数字应为红色,当我重新输入另一个小于 80 的数字时,字体颜色应为黑色。
【问题讨论】:
-
欢迎使用tour。显示您迄今为止尝试过的代码,特别是您遇到问题的地方。
-
使用 TextChanged 事件
标签: vb.net
基本上我想在文本框中输入一个数字。如果数字大于 80,则数字应为红色,当我重新输入另一个小于 80 的数字时,字体颜色应为黑色。
【问题讨论】:
标签: vb.net
如果你输入一个数字,你首先要仔细检查,以免出现异常,以下是添加 textbox1 后需要进行的操作:
'The event that handle text change
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim myInteger As Integer
'first check if your textbox1 value is a valid integer
If Integer.TryParse(TextBox1.Text, myInteger) Then
'use if ... else to check the value
If myInteger > 80 Then
' give the color
TextBox1.ForeColor = Color.Red
Else
TextBox1.ForeColor = Color.Black
End If
End If
End Sub
这里的代码假设你正在寻找一个整数而不是小数,你说:
大于 80 ... ...另一个小于 80 的数字
= 80 怎么样?
【讨论】: