Control.TextChanged 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
發生在 Text 屬性值變更時。
public:
event EventHandler ^ TextChanged;
public event EventHandler TextChanged;
public event EventHandler? TextChanged;
member this.TextChanged : EventHandler
Public Custom Event TextChanged As EventHandler
事件類型
範例
下列程式碼範例會變更 ForeColor TextBox 顯示貨幣資料的 。 此範例會將文字轉換成十進位數,如果數位為負數,則將 變更為 Color.Black ,如果數位為正數,則為 。 ForeColor Color.Red 此範例需要您有 Form 包含 的 TextBox 。
private:
void currencyTextBox_TextChanged( Object^ /*sender*/, EventArgs^ /*e*/ )
{
try
{
// Convert the text to a Double and determine if it is a negative number.
if ( Double::Parse( currencyTextBox->Text ) < 0 )
{
// 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;
}
}
catch ( Exception^ )
{
// If there is an error, display the text using the system colors.
currencyTextBox->ForeColor = SystemColors::ControlText;
}
}
private void currencyTextBox_TextChanged(object sender, EventArgs e)
{
try
{
// Convert the text to a Double and determine if it is a negative number.
if(double.Parse(currencyTextBox.Text) < 0)
{
// 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;
}
}
catch
{
// If there is an error, display the text using the system colors.
currencyTextBox.ForeColor = SystemColors.ControlText;
}
}
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
備註
如果屬性是透過程式設計修改或使用者互動來變更, Text 就會引發這個事件。
如需處理事件的詳細資訊,請參閱 處理和引發事件。