BrowsableAttribute 類別

定義

指定屬性 (Property) 或事件是否應該顯示於 [屬性] 視窗中。

public ref class BrowsableAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.All)]
public sealed class BrowsableAttribute : Attribute
public sealed class BrowsableAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.All)>]
type BrowsableAttribute = class
    inherit Attribute
type BrowsableAttribute = class
    inherit Attribute
Public NotInheritable Class BrowsableAttribute
Inherits Attribute
繼承
BrowsableAttribute
屬性

範例

下列範例會將屬性標示為可流覽。

public:
   [Browsable(true)]
   property int MyProperty 
   {
      int get()
      {
         // Insert code here.
         return 0;
      }
      void set( int value )
      {
         // Insert code here.
      }
   }
[Browsable(true)]
public int MyProperty
{
    get
    {
        // Insert code here.
        return 0;
    }
    set
    {
        // Insert code here.
    }
}

<Browsable(True)> _
Public Property MyProperty() As Integer
    Get
        ' Insert code here.
        Return 0
    End Get
    Set
        ' Insert code here.
    End Set 
End Property

下一個範例示範如何檢查 BrowsableAttribute 的值 MyProperty 。 首先,程式碼會取得 PropertyDescriptorCollection 具有 物件之所有屬性的 。 接下來,程式碼會索引到 PropertyDescriptorCollection ,以取得 MyProperty 。 然後它會傳回此屬性的屬性,並將其儲存在屬性變數中。

此範例提供兩種檢查 值 BrowsableAttribute 的不同方式。 在第二個程式碼片段中,此範例會呼叫 Equals 方法。 在最後一個程式碼片段中,此範例會使用 Browsable 屬性來檢查值。

// Gets the attributes for the property.
AttributeCollection^ attributes = TypeDescriptor::GetProperties( this )[ "MyProperty" ]->Attributes;

// Checks to see if the value of the BrowsableAttribute is Yes.
if ( attributes[ BrowsableAttribute::typeid ]->Equals( BrowsableAttribute::Yes ) )
{
   
   // Insert code here.
}

// This is another way to see whether the property is browsable.
BrowsableAttribute^ myAttribute = dynamic_cast<BrowsableAttribute^>(attributes[ BrowsableAttribute::typeid ]);
if ( myAttribute->Browsable )
{
   // Insert code here.
}
// Gets the attributes for the property.
AttributeCollection attributes =
   TypeDescriptor.GetProperties(this)["MyProperty"].Attributes;

// Checks to see if the value of the BrowsableAttribute is Yes.
if (attributes[typeof(BrowsableAttribute)].Equals(BrowsableAttribute.Yes))
{
    // Insert code here.
}

// This is another way to see whether the property is browsable.
BrowsableAttribute myAttribute =
   (BrowsableAttribute)attributes[typeof(BrowsableAttribute)];
if (myAttribute.Browsable)
{
    // Insert code here.
}
' Gets the attributes for the property.
Dim attributes As AttributeCollection = _
    TypeDescriptor.GetProperties(Me)("MyProperty").Attributes

' Checks to see if the value of the BrowsableAttribute is Yes.
If attributes(GetType(BrowsableAttribute)).Equals(BrowsableAttribute.Yes) Then
    ' Insert code here.
End If 

' This is another way to see whether the property is browsable.
Dim myAttribute As BrowsableAttribute = _
    CType(attributes(GetType(BrowsableAttribute)), BrowsableAttribute)
If myAttribute.Browsable Then
    ' Insert code here.
End If

如果您使用 標記類別 BrowsableAttribute ,請使用下列程式碼來檢查值。

AttributeCollection^ attributes = TypeDescriptor::GetAttributes( MyProperty );
if ( attributes[ BrowsableAttribute::typeid ]->Equals( BrowsableAttribute::Yes ) )
{
   // Insert code here.
}
AttributeCollection attributes =
    TypeDescriptor.GetAttributes(MyProperty);
if (attributes[typeof(BrowsableAttribute)].Equals(BrowsableAttribute.Yes))
{
    // Insert code here.
}
Dim attributes As AttributeCollection = TypeDescriptor.GetAttributes(MyProperty)
If attributes(GetType(BrowsableAttribute)).Equals(BrowsableAttribute.Yes) Then
    ' Insert code here.
End If

備註

視覺化設計工具通常會顯示在屬性視窗沒有可流覽屬性或標示 BrowsableAttribute 為 建構函 browsable 式參數 true 的成員。 這些成員可以在設計階段修改。 標示為 BrowsableAttributebrowsable 構函式參數 false 的成員不適用於設計階段編輯,因此不會顯示在視覺化設計工具中。 預設為 true

注意

當您使用 Browsable(true) 標記屬性時,這個屬性的值會設定為常數成員 Yes 。 對於標示 Browsable(false) 為 的屬性,值為 No 。 因此,當您在程式碼中檢查此屬性的值時,您必須將 屬性指定為 BrowsableAttribute.YesBrowsableAttribute.No

如需詳細資訊,請參閱屬性

建構函式

BrowsableAttribute(Boolean)

初始化 BrowsableAttribute 類別的新執行個體。

欄位

Default

指定 BrowsableAttribute 的預設值,就是 Yes。 這個 static 欄位是唯讀的。

No

指定在設計階段不可以修改的屬性或事件。 這個 static 欄位是唯讀的。

Yes

指定在設計階段可以修改的屬性或事件。 這個 static 欄位是唯讀的。

屬性

Browsable

取得值,表示物件是否為可瀏覽。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

指示這個執行個體和指定的物件是否相等。

GetHashCode()

傳回這個執行個體的雜湊碼。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

判斷這個屬性是否為預設值。

IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱