1
votes

I'm having some problems in VS2019 with UserControls.

I have a base user control (WinForms) that I want all my other User Controls to be inherited from. THe base control provides a number of common properties that I need.

public partial class MyBaseControl : System.Windows.Forms.UserControl
{

    internal Int32 _caseID;
    internal object _objectID;

    public int CaseID { get => _caseID; set => _caseID = value; }
    public object ObjectID { get => _objectID; set => _objectID = value; }

    internal virtual void MakeScreenReadOnly()
    {
    }
    public MyBaseControl()
    {
        Type systemType = this.GetType();
        PropertyInfo propertyInfo = systemType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
        this.AutoScroll = true;

    }

}

The controls that inherit this base control all work fine when the application runs, except I can't view the control in the designer. The user control is

public partial class AddressControl : MyBaseControl
    {
....
}

I get the error

The service System.Windows.Forms.Design.IEventHandlerService already exists in the service container. Parameter name: serviceType

when trying to view AddressControl in the designer.

This used to work, in earlier version of Visual Studio. Does anyone have any ideas ?

2
Something interesting I have discovered. THe solution consists of 3 projects, a Data Access library (.Net Framework), UI (the WinForms project containing the controls as detailed) and a support Library (.NET Core). If I remove the Support Library and reference the compiled DLL, it seems to be all fine. If I reference the project, that is when I get projects. The UI projects references both the Data Access and the Support librarys, and the Support library references the Data Access library - Ian Boggs
shouldnt the user control constructor contain InitializeComponent();? - Democrats
I have added it but it didn't fix the issue - Ian Boggs
did you do a clean rebuild after? - Democrats
Remove that reflection thing. In a UserControl you can set this.DoubleBuffered = true; or this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); in its constructor. -- See that you don't have duplicated attributes, e.g., [Designer(GetType(...)] in both base and derived classes. In this case, remove the attribute form the derived class. -- Don't do anything Design-time/Design-Mode related in the base class (check if (DesignMode) (...)), especially in the Load event. If something needs to be set, override OnHandleCreated() or OnLayout() or both. - Jimi

2 Answers

0
votes
  • Quit from Visual Studio.
  • Open File Explorer: navigate to the folder that contains your project.
  • Delete the “bin” and “obj” folders.
  • Open Visual Studio again and open and recompile the project.
0
votes

You can try to recreate the user control.

Exclude the old control from the project to keep it on disk.

Move the old files outside the project folder (MyBaseControl.cs & MyBaseControl.Designer.cs).

Use the Project > Add > User control wizard.

Copy your old code in it.

And don't remove the InitializeComponent(); from the constructor: add your code after.

It may work and solve your problem if Visual Studio is not corrupted else you can try repair or to uninstall and reinstall it.

Here what I tried:

  • I added this usercontrol:

    public partial class UserControl1 : UserControl
    {
      public UserControl1()
      {
        InitializeComponent();
      }
    }
    
  • I compile.

  • I added another usercontrol and change the parent:

    public partial class UserControl2 : UserControl1
    {
      public UserControl2()
      {
        InitializeComponent();
      }
    }
    

It works fine and the designer display both correctly.

Next I added your code while keeping the InitializeComponent() call:

public partial class UserControl1 : UserControl
{
  internal Int32 _caseID;
  internal object _objectID;

  public int CaseID { get => _caseID; set => _caseID = value; }
  public object ObjectID { get => _objectID; set => _objectID = value; }

  internal virtual void MakeScreenReadOnly()
  {
  }
  public UserControl1()
  {
    InitializeComponent();
    Type systemType = this.GetType();
    PropertyInfo propertyInfo = systemType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
    this.AutoScroll = true;
  }
}

I compile.

All works fine too.