Control.BackColorChanged イベントとは何? わかりやすく解説 Weblio辞書 (original) (raw)

Text プロパティの値が変更されたときに実行されるイベント ハンドラコード例次に示しますControl クラスには、PropertyNameChanged という名前のパターンを持つメソッドいくつかあります。これらは、対応する PropertyName の値が変更されたときに生成されます。ここで、PropertyName対応するプロパティの名前を表します

通貨型のデータ表示する TextBox の ForeColor を変更するコード例次に示しますテキスト10 進数変換し、値が負の場合ForeColor を Color.Red に変更し、正の場合は Color.Black に変更する例を次に示します。この例では、**TextBox** を格納する Form存在している必要があります

Visual Basic

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

C#

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; } }

C++

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; } }

J#

private void currencyTextBox_TextChanged(Object sender, EventArgs e) { try { // Convert the text to a Double and determine if it is a // negative number. if (System.Double.Parse(currencyTextBox.get_Text()) < 0) { // If the number is negative, display it in Red. currencyTextBox.set_ForeColor(Color.get_Red()); } else { // If the number is not negative, display it in Black. currencyTextBox.set_ForeColor(Color.get_Black()); } } catch (System.Exception exp) { // If there is an error, display the text using the system colors. currencyTextBox.set_ForeColor(SystemColors.get_ControlText()); } } //currencyTextBox_TextChanged