ElektroStudios
ElektroStudios

Reputation: 20464

How to properly subclass an UserControl in this way?

SCENARIO

I have a subclassed NumericDown like this:

Public Class MyNumericUpDown : Inherits NumericUpDown

  ' More code here that does not matter...

End Class

I would like to compile it as an UserControl of a WindowsForms Control Library project to have the beneffit of the UserControl's property grid when debugging the project.

PROBLEM

I can't find the way to compile my NumericDown as I want without breaking the auto-generated Usercontrol class of the WindowsForms Control Library project, this means, breaking the property grid feature and after compiling the project having a final exception message saying me that my dll: doesn't contain any UserControl types (but really there is and I can add it into the VS control toolbox).

QUESTION

In C# or VB, how I can properly white an WindowsForms Control Library project to show only my custom NumericDown without loosing the property grid feature?

I hope you could understand what I want.

I'll try to say it in other words: I only would like to test my NumericUpDown in the property grid, not the usercontrol generated by default by the WindowsForms Control Library project.

It should be an "unit" when adding the dll into the VisualStudio control toolbox but instead that I get two separated controls.

CODE:

I don't have anything better to show because I can't find info to start doing this.

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

Upvotes: 1

Views: 665

Answers (1)

The test application that is shipped with Visual Studio will only search for controls derived from UserControl. If you want it to be able to view/test other types then you need to create a custom application.

The following code is just a proof of concept. The easiest way to implement a full working application is to simply drop the UserControlTestContainer.exe onto a decompiler like reflector and copy the code.

  1. Create a custom windows forms application and name it UserControlTestContainer.
  2. Create a shared sub main.
  3. Uncheck enable application framework and set the startup object to sub main.
  4. When your code looks like mine, build.
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. Replace the default UserControlTestContainer.exe located in C:\Program Files (x86)\Microsoft Visual Studio {version}\Common7\IDE with your custom UserControlTestContainer.exe.
  2. Back in the windows forms control library create a custom control derived from Control and hit run.

Custom UserControl Test Container

Upvotes: 1

Related Questions