下面的代码示例是一个事件处理程序,该处理程序在 Text 属性值更改时执行。 Control 类有几个名称模式为PropertyNameChanged的方法,它们在相应的PropertyName值(PropertyName表示相应的属性名)更改时引发。
- Private Sub currencyTextBox_TextChanged(sender As Object,_
- e As EventArgs) Handles currencyTextBox.TextChanged
- Try
- ' Convert the text to a Double and determine if it is a negative number.
- If Double.Parse(currencyTextBox.Text) < 0 Then
- ' If the number is negative,display it in Red.
- currencyTextBox.ForeColor = Color.Red
- Else
- ' If the number is not negative,display it in Black.
- currencyTextBox.ForeColor = Color.Black
- End If
- Catch
- ' If there is an error,display the text using the system colors.
- currencyTextBox.ForeColor = SystemColors.ControlText
- End Try
- End Sub