[英]How to properly subclass an UserControl in this way?
情景
我有一個子類的NumericDown像這樣:
Public Class MyNumericUpDown : Inherits NumericUpDown
' More code here that does not matter...
End Class
我想將其編譯為WindowsForms控件庫項目的UserControl
,以在調試項目時獲得UserControl屬性網格的好處。
問題
我找不到不破壞WindowsForms控件庫項目的自動生成的Usercontrol
類的方式來編譯我的NumericDown的方法,這意味着要破壞屬性網格功能,並且在編譯項目后會有一條最終異常消息說我我的dll: doesn't contain any UserControl types
(但確實存在,我可以將其添加到VS控件工具箱中)。
題
在C#或VB中,如何正確地將WindowsForms控件庫項目加白以僅顯示我的自定義NumericDown而不丟失屬性網格功能?
我希望你能理解我想要的。
我將嘗試用另一種方式說:我只想在屬性網格中測試我的NumericUpDown,而不是WindowsForms控件庫項目默認生成的usercontrol。
將dll添加到VisualStudio控件工具箱中時,它應該是一個“單元”,但是我得到了兩個單獨的控件。
碼:
我沒有更好的表現,因為我找不到開始這樣做的信息。
Public Class UserControl1 : Inherits UserControl
Public Sub New()
InitializeComponent()
' This is not what I want,
' or at least I think it shouldn't be done as normally like this,
' I only want to use and see my custom NumericUpDown on the property grid,
' not depending on any UserControl ControllCollection.
Me.Controls.Add(New MyNumericUpDown)
End Sub
End Class
Visual Studio附帶的測試應用程序將僅搜索從UserControl派生的控件 。 如果希望它能夠查看/測試其他類型,則需要創建一個自定義應用程序。
以下代碼僅是概念證明 。 實現完整的應用程序的最簡單方法是將UserControlTestContainer.exe
放到反編譯器(如反射器)上並復制代碼。
UserControlTestContainer
。 enable application framework
,並將startup object
設置為sub main
。 Public Class Form1
Public Sub New(Optional ByVal args As String() = Nothing)
Me.InitializeComponent()
Me.args = New Label With {.Dock = DockStyle.Fill, .Text = If((args Is Nothing), "(null)", String.Join(Environment.NewLine, args))}
Me.Controls.Add(Me.args)
End Sub
<STAThread()>
Public Shared Sub Main(Optional ByVal args As String() = Nothing)
Application.EnableVisualStyles()
Application.Run(New Form1(args))
End Sub
Private args As Label
End Class
C:\\Program Files (x86)\\Microsoft Visual Studio {version}\\Common7\\IDE
的默認UserControlTestContainer.exe
替換為自定義UserControlTestContainer.exe
。 Control
的自定義控件,然后單擊運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.