User Control Inheritance and Namespaces

I've noticed two unusual issues with Windows Forms user-controls in Visual Basic and was wondering if there was a workaround for either of these:

You don't seem to be able to place a user-control in a namespace other than the project's root namespace. When you add the namespace to the class file you receive the error:

The class UserControl1 can be designed, but is not the first class in the file. Visual Studio requires that designers use the first class in the file. Move the class code so that it is the first class in the file and try loading the designer again.

I assume this is because the partial class is in a different namespace, is there a way to influence what namespace is used for the partial class

You can't have a user-control inherit from another class, even if that class inherits from UserControl. Your receive the error:

Base class 'Test' specified for class 'UserControl1' cannot be different from the base class 'System.Windows.Forms.UserControl' of one of its other partial types. C:\Devel\WindowsApplication1\WindowsApplication1\Editors\UserControl1.vb 2 14 WindowsApplication1


This makes it very ugly to share non-visual functionality between a group of user-controls.



Answer this question

User Control Inheritance and Namespaces

  • Art Vandolay Jr.

    Seems like a bug to me... It happens in SP1 as well.
  • Elham Sarikhani

    Right-click the Project in the Solution Explorer and choose Properties. There you can change the namespace so that all files in that project will use the new namespace when you create them.

  • I Mrus

    Could you post some code

  • Bilberry71

    Ah... indeed. VB.NET ignores the folder structure. I wonder if it's a bug or it's by design. So it seems you will need to manually adjust the namespaces for both the code file and designer file.


  • Daudi

    I did create a folder and the UserControl was still created in the application's root namespace not the folder's namespace, which is why I tried specifying my own.
  • ascanio

    You can also manually edit the namespace of the partial class. Select "Show All Files" for the project, and a plus sign will appear next to the form. Expand it, and you will see the designer file for your form. Add the namespace there. In my test, the namespace was preserved, even after making and saving changes to the form (which regenerated the partial class). I couldn't guarantee that would always be the case though.

  • Jonas.S

    Normally namespaces should follow the directory structure so if you have ProjectX with the default ProjectX namespace adding a form in a folder called FolderY would result in the form using the namespace ProjectX.FolderY.

    If you still need to specify your own namespace then you need to modify designer generated file to be in the same namespace. To do this press the second button in the toolbar above the solution explorer tree (the tooltip of that button says "Show all files". This will allow you to see the designer generated file in the solution explorer so you can open it and add your namespace.

    Same thing for inheritance. You need to edit the designer generated file to also inherit from your base class.

    Given the fact that usually is not recommended to modify the designer generated file perhaps it's better to use folders to put forms in different namespaces and use the Inherited User Control template when you want to create a new user control that inherits from another class than UserControl.


  • User Control Inheritance and Namespaces