簡體   English   中英

如何以這種方式正確地繼承UserControl?

[英]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放到反編譯器(如反射器)上並復制代碼。

  1. 創建一個自定義Windows窗體應用程序,並將其命名為UserControlTestContainer
  2. 創建一個共享子主。
  3. 取消選中enable application framework ,並將startup object設置為sub main
  4. 當您的代碼看起來像我的代碼時,進行構建。
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
  1. C:\\Program Files (x86)\\Microsoft Visual Studio {version}\\Common7\\IDE的默認UserControlTestContainer.exe替換為自定義UserControlTestContainer.exe
  2. 回到Windows窗體控件庫,創建一個派生自Control的自定義控件,然后單擊運行。

自定義UserControl測試容器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM