【问题标题】:to change font color int the textbox for a certain range of number在文本框中更改特定数字范围的字体颜色
【发布时间】:2020-12-27 17:05:32
【问题描述】:

基本上我想在文本框中输入一个数字。如果数字大于 80,则数字应为红色,当我重新输入另一个小于 80 的数字时,字体颜色应为黑色。

【问题讨论】:

  • 欢迎使用tour。显示您迄今为止尝试过的代码,特别是您遇到问题的地方。
  • 使用 TextChanged 事件

标签: vb.net


【解决方案1】:

如果你输入一个数字,你首先要仔细检查,以免出现异常,以下是添加 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 怎么样?

【讨论】:

  • 好答案,但如果我们回答质量差的问题,新用户永远学不会问好问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
相关资源
最近更新 更多