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

6

14

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

Kendrick answered 18/7, 2014 at 9:35 Comment(0)
D
24

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!

Decoder answered 21/12, 2014 at 17:55 Comment(3)
I tried but it does not work. I closed Visual Studio, deleted folders, reopened and recompiled be described. I tried also to clean Fully Solution. Nothing to do.Hypothecate
Worked for me as well. I also just only had to delete the obj folder and not the bin folderGuddle
I closed VS and re-opened without deleting anything and it worked !!!! +1Marthmartha
O
6

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
Olfactory answered 9/5, 2016 at 16:5 Comment(1)
In my case there was code restoring form layout in parent's OnLoad. Apparently it didn't work well with WinForms designer.Cozy
R
0

I just ran into this problem as well. I tried the above solution, and it didn't work for me. The structure I have is this:

public Form1 : Form

public Form2 : Form1

public Form3 : Form2

I tried rebuilding and deleting the obj/bin folders but could not get this error to go away. Finally, as a sanity check, I changed Form3 to inherit from the Windows Form class:

System.Windows.Forms.Form

Then I reopened Form3 in the designer, and it appeared (as I expected). I then changed Form3 back to inherit from Form2 and reopened Form3 in the designer. And it worked.

#random-bug-fix-win

Good luck!

Recite answered 19/1, 2015 at 6:53 Comment(0)
S
0

I cleaned and rebuilt my solution to get off this situation.

Also check if you used extended class for Form and also using KeyEvent or MouseEvent from Windows.Forms, sure that you call this args by Windows.Forms.KeyEventArgs or Windows.Forms.MouseEventArgs.

Smukler answered 28/11, 2015 at 10:36 Comment(0)
U
0

Check whether some event is going to call at opening or closing even of that specific form , and once you identify that event simply past the following statement . Hopefully it will work for you.

If (DesignMode) Then Exit Sub
Uttica answered 24/4, 2018 at 11:18 Comment(0)
C
0

If you use a lot of pinned classes like me, (.net<5) then simply closing that tab and reopening that tab might fix your problem.

Catenane answered 29/4, 2024 at 13:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.