Windows フォーム コントロールのプロパティの定義

プロパティの概要については、「プロパティの概要」を参照してください。 プロパティを定義するときには、いくつかの重要な考慮事項があります。

  • 定義するプロパティに属性を適用する必要があります。 属性によって、デザイナーでプロパティがどのように表示されるかが指定されます。 詳細については、「コンポーネントのデザイン時属性」を参照してください。

  • プロパティの変更がコントロールのビジュアル表示に影響する場合は、set アクセサーから (コントロールによって Control から継承される) Invalidate メソッドを呼び出します。 次に Invalidate によって OnPaint メソッドが呼び出されます。これにより、コントロールが再描画されます。 Invalidate を複数回呼び出しても、効率のため OnPaint が呼び出されるのは 1 回です。

  • .NET Framework クラス ライブラリでは、整数、10 進数、ブール値など、一般的なデータ型に対応する型コンバーターを使用できます。 型コンバーターは、一般に文字列から値への変換を行うために使用されます (文字列データから他のデータ型に変換)。 一般的なデータ型は、値を文字列に変換し、文字列を適切なデータ型に変換する既定の型コンバーターに関連付けられています。 カスタム (つまり、非標準的な) データ型であるプロパティを定義する場合、そのプロパティに関連付けられる型コンバーターを指定する属性を適用する必要があります。 また、属性を使用してカスタム UI 型エディターとプロパティを関連付けることもできます。 UI 型エディターには、プロパティやデータ型を編集するためのユーザー インターフェイスが備わっています。 たとえば、カラー ピッカーなどの UI 型エディターがあります。 属性の例は、このトピックの最後に記載されています。

    注意

    型コンバーターまたは UI 型エディターをカスタム プロパティに使用できない場合、「デザイン時サポートの拡張」に示されているものを実装できます。

次のコード フラグメントは、カスタム コントロール FlashTrackBar に対して EndColor という名前のカスタム プロパティを定義します。

Public Class FlashTrackBar  
   Inherits Control  
   ...  
   ' Private data member that backs the EndColor property.  
   Private _endColor As Color = Color.LimeGreen  
  
   ' The Category attribute tells the designer to display  
   ' it in the Flash grouping.
   ' The Description attribute provides a description of  
   ' the property.
   <Category("Flash"), _  
   Description("The ending color of the bar.")>  _  
   Public Property EndColor() As Color  
      ' The public property EndColor accesses _endColor.  
      Get  
         Return _endColor  
      End Get  
      Set  
         _endColor = value  
         If Not (baseBackground Is Nothing) And showGradient Then  
            baseBackground.Dispose()  
            baseBackground = Nothing  
         End If  
         ' The Invalidate method calls the OnPaint method, which redraws
         ' the control.  
         Invalidate()  
      End Set  
   End Property  
   ...  
End Class  
public class FlashTrackBar : Control {  
   ...  
   // Private data member that backs the EndColor property.  
   private Color endColor = Color.LimeGreen;  
   // The Category attribute tells the designer to display  
   // it in the Flash grouping.
   // The Description attribute provides a description of  
   // the property.
   [  
   Category("Flash"),  
   Description("The ending color of the bar.")  
   ]  
   // The public property EndColor accesses endColor.  
   public Color EndColor {  
      get {  
         return endColor;  
      }  
      set {  
         endColor = value;  
         if (baseBackground != null && showGradient) {  
            baseBackground.Dispose();  
            baseBackground = null;  
         }  
         // The Invalidate method calls the OnPaint method, which redraws
         // the control.  
         Invalidate();  
      }  
   }  
   ...  
}  

次のコード フラグメントは、型コンバーターと UI 型エディターをプロパティ Value に関連付けます。 この場合、Value は整数であり、既定の型コンバーターを持ちますが、TypeConverterAttribute 属性は、デザイナーがこれをパーセントで表示できるようにするカスタム型コンバーター (FlashTrackBarValueConverter) を適用します。 UI 型エディター FlashTrackBarValueEditor により、そのパーセントを視覚的に表示できます。 この例は、TypeConverterAttribute または EditorAttribute 属性で指定された型コンバーターまたはエディターによって、既定のコンバーターがオーバーライドされることも示しています。

<Category("Flash"), _  
TypeConverter(GetType(FlashTrackBarValueConverter)), _  
Editor(GetType(FlashTrackBarValueEditor), _  
GetType(UITypeEditor)), _  
Description("The current value of the track bar.  You can enter an actual value or a percentage.")>  _  
Public ReadOnly Property Value() As Integer  
...  
End Property  
[  
Category("Flash"),
TypeConverter(typeof(FlashTrackBarValueConverter)),  
Editor(typeof(FlashTrackBarValueEditor), typeof(UITypeEditor)),  
Description("The current value of the track bar.  You can enter an actual value or a percentage.")  
]  
public int Value {  
...  
}  

関連項目