Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Place User Control on Form

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 rebuild the project, the new control appears in the toolbox. And when I drag the control from the toolbox onto a form, I get the following error.

Failed to load toolbox item 'TagGroup'. It will be removed from the toolbox.

This happened the only other time I created a user control as well. I've searched the web but most answers I found seemed related to having the control in a separate assembly. (Note that I found plenty of questions with the same problem I'm having.)

Can anyone suggest where I should look next?

like image 686
Jonathan Wood Avatar asked Jan 26 '11 00:01

Jonathan Wood


People also ask

How do you place the controls on a form?

Add the control by drawing Select the control by clicking on it. In your form, drag-select a region. The control will be placed to fit the size of the region you selected.

What is user control form?

User controls are a way of making a custom, reusable component. A user control can contain other controls but must be hosted by a form.


2 Answers

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:

  • MyApp which is my main project (Windows Form Application) compiled in 64-bit and referencing my second project
  • MyApp.UI.UserControls (class library) is compiled for "Any CPU" and contains all my user controls

Works like a charm and it's clean


By the way, there is a Microsoft support article about that issue.

  • Action: You attempt to use a 64-bit component within the Microsoft Visual Studio Integrated Development Environment (IDE).
  • Error cause: This behavior is by design. Visual Studio is a 32-bit process, and therefore can only execute 32-bit modules. While Visual Studio allows you to add a reference to a 64-bit assembly, it cannot actually JIT compile it to 64-bit and execute it in process.
  • Resolution:
    1. Rebuild the assembly using the "AnyCPU" setting. This would allow the component to run within a 32-bit process (such as Visual Studio), or in a 64-bit process.
    2. Add the assembly as a reference and load the control dynamically at run-time. Although you still would be unable to use the control within any designer inside Visual Studio, you can still write the code needed to instantiate the control and set it's properties accordingly.

Source: http://support.microsoft.com/kb/963017

like image 164
Yves M. Avatar answered Oct 07 '22 21:10

Yves M.


I finally figured this one out.

The project I'm working with uses two class-library assemblies. Although these have nothing to do with the control I'm discussing, I looked and saw both libraries have Platform Target in the Properties|Build tab set to "Any CPU".

On the other hand, my application had this setting set to "x64". By changing my application's setting to "Any CPU", I can now place my user controls onto my forms.

Go figure...

like image 31
Jonathan Wood Avatar answered Oct 07 '22 22:10

Jonathan Wood