简体   繁体   中英

C# WinForms Base UserControl

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?

  • 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM