2015-09-02 40 views
1

我正在用vb.net開發我自己的userControl。我對這項任務很感興趣。從UserControl中刪除屬性和事件vb.net

我想刪除默認屬性。 谷歌後,我發現了幾個主題,像這樣: Removing certain properties in a user control, i.e. forcing one value and not editable in Design mode

所以,我試圖使用它,但不適用於我。我不知道我錯過了什麼,或者錯了什麼。

Public Class MyControlDesigner 
    Inherits System.Windows.Forms.Design.ControlDesigner 

    Protected Overrides Sub PreFilterProperties(ByVal properties As System.Collections.IDictionary) 
     MyBase.PreFilterProperties(properties) 
     properties.Remove("BackColor") 
     properties.Remove("ForeColor") 
     properties.Remove("Font") 
    End Sub 
End Class 

    <DesignerAttribute(GetType(MyControlDesigner))> _ 
Public Class MyUserControl 
    ' ... 
End Class 

要隱藏覆蓋性能我按照這個話題Hiding inherited properties這工作得很好,對於其中的一些。

<Browsable(False), EditorBrowsable(EditorBrowsableState.Never)> _ 
Public Shadows Property AutoScroll() As Boolean 
    Get 
     Return m_AutoScroll 
    End Get 
    Set(ByVal value As Boolean) 
     m_AutoScroll = value 
    End Set 
End Property 

但仍然有其他屬性,我不知道如何隱藏或刪除。像字體,前景色,保證金等等

得益於先進的

編輯:一旦我完成我的控制,我不希望看到的,所有喜歡的圖片屬性,只有我想展示mine'秒。

image

編輯:從@Plutonix添加代碼

Create a new class

Implements new class

Testing

+0

你在嘗試從intellisense,用戶代碼或屬性編輯器中刪除屬性。解釋「它不適合我」的含義。事件不能被刪除;在某些情況下(子分類控制),你可以阻止它們發射,但它們仍然存在 – Plutonix

+0

你在嘗試從intellisense,用戶代碼或屬性編輯器中刪除屬性。我試圖從我的編輯器中刪除,在這種情況下,從WinCC(西門子的Scada) – yaqui

回答

1

我沒有訪問該控制/工具/屬性編輯器,但您可以嘗試使用TypeConverter。這適用於從UserControl繼承的控件,用於隱藏屬性網格中的屬性,但不會將它們從VS IDE屬性編輯器中隱藏。

VS IDE使用反射來獲取屬性列表,顯然忽略了TypeConverter。如果你的工具做了類似的事情,這不會起作用 - 再一次,我沒有工具來測試它,但它很簡單,值得一試。

我創建了一個具有幾個控件的實際UserControl。然後:

Imports System.ComponentModel 

Public Class YControlConverter 
    Inherits TypeConverter 

    Public Overrides Function GetPropertiesSupported(context As ITypeDescriptorContext) As Boolean 
     Return True 
    End Function 

    Public Overrides Function GetProperties(context As ITypeDescriptorContext, 
          value As Object, 
          attributes() As Attribute) As PropertyDescriptorCollection 

     Dim propNames() As String = {"backcolor", "forecolor", 
           "autoscroll", "autoscrollminsize", 
           "autoscrollmargin", "autoscrolloffset", 
           "autoscrollposition"} 

     Dim pdc As PropertyDescriptorCollection = TypeDescriptor.GetProperties(context.Instance) 
     ' collection to store the ones we want: 
     Dim myPDCList As New List(Of PropertyDescriptor) 

     For Each pd As PropertyDescriptor In pdc 
      If propNames.Contains(pd.Name.ToLowerInvariant) = False Then 
       myPDCList.Add(pd) 
      End If 
     Next 

     Return New PropertyDescriptorCollection(myPDCList.ToArray()) 

    End Function 
End Class 

然後與TypeConverter裝飾你的用戶控件:

<TypeConverter(GetType(YControlConverter))> 
Public Class YControl 

這基本上運行的直通爲PropertyDescriptorCollection控制和返回新的集合之前過濾掉不需要的屬性。如果有效,只需將名稱添加到您要隱藏的propNames陣列。查看在PropertyGrid

enter image description here

正如你可以看到,所有的AutoScroll...屬性被刪除,以及BackColor。其他人也不見了。如果編輯器將使用您的TypeConverter而不是反射,它應該工作。

-

如何您TypeConverter使用PropertyGrid中進行測試。使用表格屬性網格和一個按鈕,在按鈕點擊:

Dim yp As New YControl 

PropertyGrid1.SelectedObject = yp 

如果AutoScroll...屬性是從道具格失蹤,您的TypeConverter的作品!如果他們仍然顯示在其他工具中,它使用反射像VS.

+0

非常感謝您的示例。 :-)。我試圖實現你的代碼,就像圖片一樣。但是,對不起,我不知道我做錯了什麼。我創建了一個新的類YControlConverter,然後應用於我的類控件。但是,正如你所看到的,我從所有物業獲得的照片都不像你的。我仍然擁有所有這些。謝謝先進的 – yaqui

+0

我可以看到你的代碼有些「問題」,可能不會直接影響到這種情況,但他們沒有幫助。 A)在NET中已經有一個名爲'Control'的類型,將你的名字命名爲相同可能會導致問題B)其中一些東西只是公共字段而不是屬性C)顯示的內容非常簡單,UserControl看起來很像矯枉過正。那件事上有多少種類型的UI控件? – Plutonix

+0

再次感謝您,在閱讀您的回覆之後,我創建了一個新的UserControl,名稱爲MyControl,問題編號爲A.之後,我刪除了每個函數和變量。而最後一件事,我幾乎刪除了所有的控件,現在,我有一些標籤,複選框和文本框,沒有任何更多的代碼,我不知道,但不爲我工作。我知道我一直在做錯事,但我真的不知道它是什麼。請你能給我你所做的項目,告訴我它是如何工作的嗎? – yaqui