2012-12-20 74 views
1

我發現了propertygrid,它可能很方便進行編輯,或者只是在我的程序中顯示一些自定義安裝數據。但是我需要一些屬性屬性是可變的。
像'readonly'屬性。在vb.net運行時更改屬性屬性

這是我到目前爲止有:

Const myPersonCat As String = "MyPerson" 
Const myDesc1 As String = "Firstname is one element" 
<CategoryAttribute(myPersonCat), _ 
DescriptionAttribute(myDesc1), _ 
[ReadOnly](myBool)> _ 
Public Property firstname() As String 
    Get 
     Return _firstname 
    End Get 
    Set(ByVal value As String) 
     If Not _firstname = value Then save_param("firstname", value, myPersonCat, myDesc1) 
     _firstname = value 
    End Set 
End Property 

Const mydesc2 As String = "but Lastname is second" 
<CategoryAttribute(myPersonCat), _ 
DescriptionAttribute(mydesc2), _ 
[ReadOnly](myBool)> _ 
Public Property lastname() As String 
    Get 
     Return _lastname 
    End Get 
    Set(ByVal value As String) 
     If Not _lastname = value Then save_param("lastname", value, myPersonCat, myDesc2) 
     _lastname = value 
    End Set 
End Property 

SAVE_PARAM是調用的函數與數據庫的基本數據保存屬性。
所有這些工作都很好。

但現在是一個問題... 在這裏有一些,不是太複雜的方式來設置'myBool'爲只讀屬性與變量,而不是常量,我可以阻止改變一些屬性可靠的情況在程序中。
也許整個類別或單個屬性?

或者,也許這裏存在一些其他方式來獲得類似的功能?

+0

**選中此問題** http://stackoverflow.com/questions/51269/change-attributes-parameter-at-runtime –

回答

0

不,沒有辦法改變屬性的值。作爲替代方案,您可以在Property Set中編寫代碼,以便在用戶嘗試設置該值時它會引發異常,同時它應該是隻讀的。

+0

你能舉一些簡短的例子嗎? –

+0

嗨史蒂夫,這裏是沒有問題如何逃離編寫只讀屬性,但關於設置屬性爲只讀在運行時。據我現在瞭解,這是沒有辦法做到這一點。 –