Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error : The service System.Windows.Forms.Design.IEventHandlerService already exists in the service container

I'm developping a Windows app based on the Windows Form template. I'm using .NET 3.5 version. In this app, the goal is that all the visual settings of the different forms can be managed from the App.Config file (the background color, the background color of the different buttons etc...).

So basically, I have a "FormBase" class, of which all my forms inherit, and this class contains code like this :

public class FormBase : Form
{
    protected override void OnLoad(EventArgs e)
    {
        BackColor = Color.FromName(ConfigurationManager.AppSettings["backColor"]);

        foreach (var item in this.Controls)
        {
            if (item is Button)
            {
                ((Button)item).BackColor = Color.FromName(ConfigurationManager.AppSettings["buttonBackground"]);
                ((Button)item).ForeColor = Color.FromName(ConfigurationManager.AppSettings["buttonText"]);
            }
            if (item is ...)
            {
                //some other code
            }
        }
    }
}

And then I have my App.Config file which contains code like :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="buttonText" value="White"/>
    <add key="buttonBackground" value="Red"/>
    <add key="backColor" value="White"/>
    <add key="textColor" value="Red"/>
  </appSettings>
</configuration>

And now, in the declaration of all my forms I have the line

public partial class Form1 : FormBase

My problem is, when I run the app it runs fine, and it works, the different colors in the App.Config files are the colors displayed on my forms. But when I just look at the designer in Visual Studio without running the app, the designer can't display what the form will look like and I get the following error

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

And I don't know how to solve this. This isn't a huge problem since the app runs fine anyway, but this bothers me and I'd like to know what's happening

like image 686
DevBob Avatar asked Jul 18 '14 09:07

DevBob


2 Answers

I just ran into this problem myself. According to another webpage, this error can be fixed by closing visual studio and deleting your obj folder, and then re-opening visual studio and doing a project re-build.

Here's the page I read it from. http://www.csharp411.com/ieventhandlerservice-already-exists-in-the-service-container/

They said to delete the bin folder too but I found that I didnt have to do that. Hope this helps!

like image 179
Vance Palacio Avatar answered Sep 21 '22 03:09

Vance Palacio


This worked for me although I'd still like to better understand what was going wrong. I was creating an inherited form in Visual Studio. Apparently the Visual Studio designer calls the Load function before displaying the form. The load function in the parent window was being called and accessing a control on the form, this was throwing an object reference not set to an instance of an object (why?).

The solution for me was to add the following line of code at the beginning of the parent forms load function. I am using VB but it is similar for C#.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If (DesignMode) Then Exit Sub
like image 41
ascriven Avatar answered Sep 22 '22 03:09

ascriven