I\'ve created a C# WinForms application using VS2010. I\'m new to creating user controls so I created a new user control (as part of the same project).
When I rebuil
I also experienced this problem but the cause was different. In my case a component (form) constructor or Load event invoked a method elsewhere that used reflection to find all classes that implemented a certain interface.
While that works fine at runtime, it generated the above mentioned exception at Design Time. (Type Initialization exception with a Type Load Exception as inner exception).
Same problem here. I am guessing it is related to the fact that the VS2010 installed on x64 OS is still a 32bit program in the heart.
An alternative solution one might want to try is simply open TheFormThisUserControlIsSupposedToBeAddedTo.Designer.cs and use code to add the user control. Basically, you are doing the dirty work that the Designer is supposed to do.
It is not as difficult as it sounds, esp. considering that there are probably plenty of sample code in that file already (e.g. the buttons you added using Designer). The only difficult part is figuring out the right coordinates in the form to place the control.
The end result is that you can not see the user control in Desinger, but they are added on debugging/running.
An hour of confused, weary troubleshooting at 3:36AM is again easily resolved with a fresh mind the next day!
I fixed a simple spelling mistake in the control's filename, so it now matched the UserControl name, cleaned and re-built and Bobs your weird uncle's cat. :]
My application need to be 64-bit. In order to use custom user controls in the designer I just added a new project to my solution. This new project use the "AnyCPU" setting and contains all my user controls.
My solution contains the following projects:
Works like a charm and it's clean
By the way, there is a Microsoft support article about that issue.
Source: http://support.microsoft.com/kb/963017
I had this problem in VS2015 and the solution turned out to be simple.
I had created a user control by cutting and pasting a few existing controls from a form (with the aim of grouping them into the custom control). The custom control was okay (no compile errors), however the removal of the controls from the existing form meant that the application wouldn't compile. Of course not being able to add the new control meant that I couldn't update the code referencing the previous controls with code referencing the custom control.
All I did was hack and slash (commenting-out, creating temporary controls, etc.) so that the entire application would compile. After it compiled I found that I could drag the custom control onto the form (without the error that prompted this question). I then had to unhack and unslash so that the code properly referenced the new custom control.
GO project property then 'Build' tab and check 'Target Platform' was 'AnyCPU'.