77

I have an existing library (not a Winforms application) that supplies some Winforms to a bona-fide Windows application. Within this library, I would like to create a User Control to group some controls together. To accomplish this, I right-clicked, Add, User Control and dragged some controls onto the new User Control.

So far, so good. The User Control even has the requisite User Control icon. But dragging the new User Control from the Solution Explorer to a new blank Winform does not work (I get a circle with a line through it), and dragging it over to the Toolbox doesn't work either (even though I get a + sign when I drag it over the Toolbox).

Is there some sort of XML magic or something else I'm missing to make this work?


Note: I had some problems with Visual Studio 2008 that I managed to fix by following the workarounds that can be found here. I am now able to get User Controls I added to my existing project into the toolbox by simply rebuilding the project.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • 14
    You can try turning on Options -> Windows Forms Designer -> AutoToolboxPopulate to make this easier. That way, all you have to do is rebuild the solution for the user controls to show up in your toolbox. – Cody Gray - on strike Jan 19 '12 at 18:49
  • @CodyGray: It's already turned on, but thanks for the tip. – Robert Harvey Jan 19 '12 at 18:55
  • It is very unclear, especially `Within this library, I would like to create a User Control`. Just create your own project for that UC. If you really want to add the UC to the existing project then don't add it to the toolbox but just open the project. Build to get the existing controls added to the toolbox. – Hans Passant Jan 19 '12 at 19:07
  • @HansPassant: There is apparently something wrong with my VS environment; building the project containing the User Control doesn't add the control to the toolbox, and trying to open the "Choose Items" toolbox dialog crashes VS. – Robert Harvey Jan 19 '12 at 19:16
  • Start by resetting the toolbox, right-click + Reset. Add the library to your solution instead, using Add Reference or Add Project. – Hans Passant Jan 19 '12 at 19:30

4 Answers4

108

Assuming I understand what you mean:

  1. If your UserControl is in a library you can add this to you Toolbox using

    Toolbox -> right click -> Choose Items -> Browse

    Select your assembly with the UserControl.

  2. If the UserControl is part of your project you only need to build the entire solution. After that, your UserControl should appear in the toolbox.

In general, it is not possible to add a Control from Solution Explorer, only from the Toolbox.

Enter image description here

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
dknaack
  • 60,192
  • 27
  • 155
  • 202
  • Hmm, well my little side User Control project automatically adds the control to the toolbox, but not if I create the User Control directly in the existing project. Also, I seem to be having [this problem.](http://connect.microsoft.com/VisualStudio/feedback/details/363321/choose-items-in-toolbox-causes-visual-studio-2008-sp1-to-crash) – Robert Harvey Jan 19 '12 at 18:53
  • 2
    Did you build only your project or the entire solution ? I have no answer to the crash issue (this is not related to your question, i think), you should create a new answer for this. What do you think ? – dknaack Jan 19 '12 at 19:00
  • I tried a rebuild on the entire solution; it didn't improve things. I'm working through the crash problem now. – Robert Harvey Jan 19 '12 at 19:01
  • OK, I followed the workaround [here](http://connect.microsoft.com/VisualStudio/feedback/details/363321/choose-items-in-toolbox-causes-visual-studio-2008-sp1-to-crash) that explains how to put Devenv.EXE in safe mode, opened the "Choose Items" dialog from the toolbox, walked through all the tabs, and reset the toolbox. Everything is working now. Thanks for your help. – Robert Harvey Jan 19 '12 at 19:51
  • 3
    You can also right click and show all, if your control is in the list, but disable, you have another problem. – Thomas Jan 21 '14 at 18:08
  • One important omission to all these suggestions: You will not see a new UserControl in the toolbox if you don't have a Windows form document open within Visual Studio. The toolbox customizes what it shows depending on what you're doing, so be sure to have a form open in design more within VS before you open the toolbox. Use the toolbox's search feature to look for the name of your UserControl, and be sure you're using the right name (look at your User Control's class name)! – Jazimov Apr 09 '16 at 20:10
  • 4
    Had this problem in VS 2017. Closing VS 2017 and reopening updated the Toolbox with the UserControl. – iCode Aug 01 '18 at 19:38
  • You can see the user controls within a Visual Studio project by performing a build command... only then will it show up in the Toolbox "automatically''. (I'm using Visual Studio 2019). – Su Llewellyn Jul 11 '19 at 20:02
9

One way to get this error is trying to add a usercontrol to a form while the project is set to compile as x64. Visual Studio throws the unhelpful: "Failed to load toolbox item . It will be removed from the toolbox."

Workaround is to design with "Any CPU" and compile to x64 as necessary.

Reference: https://support.microsoft.com/en-us/kb/963017

GravityWell
  • 1,547
  • 1
  • 18
  • 22
5

I found that user controls can exist in the same project.
As others have mentioned, AutoToolboxPopulate must be set to True.
Create the desired user control.
Select Build Solution.
If the new user control doesn't show up in the toolbox, close/open Visual Studio.
If the user controls still aren't showing up in the toolbox, right click on the toolbox and select Reset Toolbox. Then select Build Solution. If they still aren't there, restart Visual Studio.
There must not be any build errors when the solution is built, otherwise new toolbox items will not be added to the toolbox.

Luke
  • 51
  • 1
  • 2
1

One user control can't be applied to it ownself. So open another winform and the one will appear in the toolbox.

Wincent
  • 29
  • 3