共用方式為


定義 Windows Form 控制項中的屬性

如需屬性的概觀,請參閱屬性概觀。 定義屬性時,有一些重要的考量︰

  • 您必須將屬性 (Attribute) 套用至您所定義的屬性 (Property)。 屬性 (Attribute) 指定設計工具如何顯示屬性 (Property)。 如需詳細資料,請參閱元件的設計階段屬性

  • 如果變更屬性會影響控制項的視覺效果顯示,請從 Invalidate 存取子呼叫 Control 方法 (您的控制項繼承自 set 的方法)。 Invalidate 接著會呼叫可重新繪製控制項的 OnPaint 方法。 對 Invalidate 的多個呼叫會導致對 OnPaint 的單一呼叫以提高效率。

  • .NET Framework Class Library 提供一般資料型別的轉換器,例如整數、十進位數字、布林值和其他型別。 型別轉換器的用途通常是將字串轉換成值 (從字串資料至其他資料型別)。 一般資料型別與預設型別轉換器相關聯,可將值轉換成字串,也可將字串轉換成適當的資料型別。 如果您定義的屬性 (Property) 是自訂 (亦即非標準) 資料型別,您必須套用屬性 (Attribute),指定要與該屬性 (Property) 相關聯的型別轉換器。 您也可以使用屬性 (Attribute),將自訂 UI 類型編輯器與屬性 (Property) 相關聯。 UI 類型編輯器提供使用者介面來編輯屬性或資料型別。 色彩選擇器是 UI 類型編輯器的一個例子。 本主題最後會提供屬性的範例。

    注意

    如果型別轉換器或 UI 類型編輯器不適用於您的自訂屬性,您可以如擴充設計階段支援所述自行實作。

下列程式碼片段示範如何定義自訂控制項 EndColor 的自訂事件 FlashTrackBar

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 可讓百分比以視覺化方式呈現。 這個範例也顯示 TypeConverterAttributeEditorAttribute 屬性所指定的類型轉換器或編輯器會覆寫預設轉換器。

<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 {  
...  
}  

另請參閱