2010-12-02 14 views
1

私はMSDN にControl.ParentChangedイベントについてhttp://msdn.microsoft.com/en-us/library/system.windows.forms.control.parentchanged(VS.71).aspxControl.ParentChangedイベントが発生すると、

を読んしかし、私は、サンプルコードを理解していない:何ParentChangedは、すべてのソースコードに出現するでありませんか?

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

私はControl.ParentChangedイベントが何であるかを理解していません。

答えて

0

イベントハンドラとして、これを登録する他の場所で、コードの別の部分があるでしょう:

currencyTextBox.ParentChanged += new EventHandler(currencyTextBox_TextChanged); 

しかし、私は同意 - メソッド名は誤解を招くです。

このイベントハンドラは、このコントロールの親コントロールを別の親コントロールに変更すると発生します。

raising and consuming eventsにお読みください。

4

Hehe、彼らはちょうど良い例を考え出すことができませんでした。代わりに一般的なFooChangedイベントハンドラを表示することでパンチしました。ええ、役に立たない。

ParentChangedイベントハンドラを自分で実装することは非常に珍しいことです。これは、WinFormsの内部で大きな問題で、BackColor、ForeColor、Fontなどのプロパティは「周囲」プロパティです。それらがデフォルトからオーバーライドされていない場合は、Parentの値を取得します。どちらが当然のことながら、親が変更されたことに気づくことが重要であることは、実際にであることを意味します。 winformsコードはすでにそれを処理しますが、あなたはそれについて心配する必要はほとんどありません。もちろん、独自のアンビエントプロパティを作成しない限り。

関連する問題