Mistake. Service.Windows.Forms.Design.IEventHandlerService already exists in the service container.

I am developing a Windows application based on the Windows Form template. I am using .NET 3.5 version. In this application, the goal is that all the visual settings of various shapes can be controlled from the App.Config file (background color, background color for different buttons, etc.).

So, basically I have a class β€œFormBase”, from which all my forms are inherited, and this class contains this code:

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 an App.Config file that 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 a line

 public partial class Form1 : FormBase 

My problem is that when I run the application, it works fine and it works, different colors in the App.Config files are the colors displayed in my forms. But when I just look at the designer in Visual Studio without starting the application, the designer cannot display how the form will look, and I get the following error:

Service.Windows.Forms.Design.IEventHandlerService already exists in the service container. Parameter Name: serviceType

And I do not know how to solve this. This is not a huge problem, as the application works fine, but it bothers me, and I would like to know what is happening

+8
inheritance c # xml winforms app-config
source share
4 answers

I myself ran into this problem. According to another web page, this error can be fixed by closing visual studio and deleting your obj folder, then reopening visual studio and reassembling the project.

Here I read this page. http://www.csharp411.com/ieventhandlerservice-already-exists-in-the-service-container/

They said to delete the bin folder too, but I found that I should not do this. Hope this helps!

+7
source share

This worked for me, although I would still like to better understand what is going wrong. I created a legacy 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 called and accessed the control in the form, it threw a link to an object that was not installed on an instance of the object (why?).

The solution for me was to add the following line of code at the beginning of the parent form load function. I use VB, but it looks like C #.

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If (DesignMode) Then Exit Sub 
+1
source share

I just ran into this problem. I tried the above solution and it did not work for me. I have a structure: public Form1: Form public Form2: Form1 public Form3: Form2

I tried to restore and delete the obj / bin folders, but could not remove this error. Finally, as a health 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). Then I changed Form3 back to inherit from Form2 and reopened Form3 in the designer. And it worked.

# random error predicate win

Good luck

0
source share

I cleaned and rebuilt my decision to get out of this situation.

Also check if the extended class has been used for the form, and also using KeyEvent or MouseEvent from Windows.Forms , make sure you call these arguments using Windows.Forms.KeyEventArgs or Windows.Forms.MouseEventArgs .

0
source share

All Articles