Control.BackColorChanged イベントとは何? わかりやすく解説 Weblio辞書 (original) (raw)
Text プロパティの値が変更されたときに実行されるイベント ハンドラのコード例を次に示します。Control クラスには、PropertyNameChanged という名前のパターンを持つメソッドがいくつかあります。これらは、対応する PropertyName の値が変更されたときに生成されます。ここで、PropertyName は対応するプロパティの名前を表します。
通貨型のデータを表示する TextBox の ForeColor を変更するコード例を次に示します。テキストを 10 進数に変換し、値が負の場合は ForeColor を Color.Red に変更し、正の場合は Color.Black に変更する例を次に示します。この例では、**TextBox** を格納する Form が存在している必要があります。
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
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: 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 (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