User Controls not showing up in the toolbox

2019-01-21 21:38发布

问题:

I have some UserControls that I created in ProjectA. I have ProjectB that has a windows form that I want to put the controls on. Both of these projects are in a single solution. There's a reference to ProjectA from ProjectB so it can "see" the UserControls.

However, the UserControls do not show up in the toolbox for me to drag to the windows form.

I've tried rebuilding. I've also deleted the 'bin' directory to force a rebuild-all.

How do I get VS2008 to populate the toolbox with my UserControls?

回答1:

Check this setting:

Tools > Options > Windows Forms Designer > General : AutoToolboxPopulate

It should be set to True for this to work.



回答2:

  1. Build your project to make sure it compiles.

  2. With the form that you want your user control on, open the toolbox, right click and select "choose items"

  3. Browse to your .exe or dll that you compiled in step 1.

  4. make sure that your user control has a tick next to it, press OK.

  5. Your user control should appear in the toolbox, so drag it onto your form.

This is adapted from Calanus's answer to a similar question.



回答3:

In my case, the AutoToolboxPopulate was already set (Visual C# 2010 Express).

However, I had to activate "Show All" from the properties of the Toolbox (right click) to actually see my new user controls.



回答4:

Up until now, I had no problem with usercontrols not showing in the toolbox. Build the project and it just shows up. Then today not working. After a search I went through following but still no joy.

  • Tools > Options > Windows Forms Designer > General : AutoToolboxPopulate
  • Tools > Options > Text Editor > XMAL > Misc : AutoToolboxPopulate
  • Reset the VS settings to default

So after a few hours of messing around trying to get it to work with no success, I created a new WPF windows project accepting the default name and added a usercontrol. Built the project and the user control appeared as it always had.

I then thought that something might be wrong with my project or wpf window file. Removed the project, created a new one and added a new control. Built the project but it didnt work.

The only thing I did different was choose a name for the project, which I included a space in the name "WPF Application".

Removed the project again and created a new one again called "WPFApplication" without the space and added a user control. Built it and the user control showed up.

If you want usercontrols to show up automatically in the toolbox on build, dont use spaces in the project name. Hopefully this post save's someone else a ton of wasted time.



回答5:

Another reason that user controls may not show up in the toolbox is if they do not have a default constructor.



回答6:

Also double check that your user controls have a valid Namespace.

I just found that controls without namespace are not placed inside the ToolBox.



回答7:

Usually you need to build the solution. That almost always works for me.



回答8:

I had the same problem. After a lot of googling I did not find anything, but by chance I found out that if you click on the toolbox while you are in the same project that you have created the user control in, and check "show all", then a group with the same name as your project will appear at the top of toolbox which you can find your user control in. Now you can add your control on your desired form!



回答9:

For someone who might be working with ToolStripItems (e.g. ToolStripStatusLabel), to actually get your derived control to show up in the dropdown menu (such as the one from StatusStrip), you need to set the class to public then build the solution.

Also don't forget the designer attribute for your class, something like this:

using System.Windows.Forms.Design;

[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.StatusStrip)]
public class MyStatusLabel : ToolStripStatusLabel
{
}

Took me a while to get it right. Hopefully this can save someone else's time.



回答10:

My control was public, it was checked in the 'Choose Toolbox Items' dialog, but it was still not showing up.

This is what finally worked for me:

Right click somewhere in the Toolbox and click 'Choose Items'. When I found my item it was already checked. I then unchecked the item and clicked okay. Next I right clicked the toolbox again and searched for my item... it was gone, so I clicked Browse, navigated to the obj/x86/debug folder and selected my exe. The item was then added correctly to the toolbox.

I think it's a VS bug.



回答11:

Apart all instructions that have been given(Tools > Options > Windows Forms Designer > General : AutoToolboxPopulate) + you need to build the solution (which is obvious for me) you will probably have to pay attention to the class access modifier: it MUST be PUBLIC. I just created a control and did not appeared in toolbox list and I did not knew why. So after set

public class  yourClass:control {}

than it appeared in list. ;) Hope this will help others.



回答12:

Check your build output directory. If for some reason you are building your output somewhere other than the project bin\ directory, your controls won't show up in your toolbox.



回答13:

Here was my problem: I had added a new constructor for my control which accepted a few arguments, but I had not explicitly re-declared the empty constructor! The toolbox can only include controls which have empty constructors. In general, when you are designing a class in vb, it has an empty constructor defined implicitly (meaning you don't need to declare one). BUT, as soon as you start designing your own constructors, this empty constructor disappears, so you need to explicitly redefine it in your code! Anyways, I realize most experienced vb coder's already know all this, but hopefully this can help some newbies like myself :).



回答14:

It is possible for all of the above to fail.

I fixed it by creating a new user control (TestControl) and it triggered Visual Studio to magically add my project's controls tab + controls back into the Toolbox. Then I just deleted the test control.

This happened to me after recently installing a VS 2008 automated windows update, by the way.



回答15:

What I usually do is create a new tab and add the exe/dll to that tab... Not too comfortable with that solution because of the load time and general hassle.

A friend showed me a way to speed this up. Instead of having to click "Choose Items..." in the toolbox,etc, for each new control you make - You can create a file named MyCustomControls and there you can create your custom controls.

Now you only have to do the "Choose Items..." and add this file ONCE. If you later on decide to add a new control, create it in MyCustomControls and then rebuild.
Then your toolbox will have your new control. (It will be displayed automatically with a regular compile if you have AutoToolboxPopulate I think)

This is unfortunate, because often you want to separate classes into "one class per file". It is horrible that you have to ruin your code architecture just because VS doesn't want to do it your way. :)

I am not too comfortable with this solution either but if you need to do something quick and you don't care about multiple user controls within a file or just are lazy, this might suit you well. :)



回答16:

Well, nothing was working for me except this worked...

  1. I create a new project in my solution, and for this project it works so I was going to start using this one
  2. Playing arond, I started tweaking the xml of the csproj file to find out what was wrong with my other project in the same solution and then I reset it as it didn't seem to be working
  3. It is now magically working again in my project

Not a very good solution but you should try these steps

  1. See if it works in a brand new solution/project.
  2. See if it works in a brand new project in the same solution
  3. If #2 worked, maybe compare .csproj files...somehow mine started working again while I was doing this(this is very frustrating).


回答17:

Providing you already tried tinkering with:

  • Tools > Options > Windows Forms Designer > General : AutoToolboxPopulate
  • Tools > Options > Text Editor > XMAL > Misc : AutoToolboxPopulate
  • created a default constructor
  • build your project / reset the toolbox and add manually your tab

Yet you still see it greyed out..

Check your path length and check the charcters being used in your absolute path.

I had a project residing in "C:\Users\myName\myCompany\R&D\Projects"
And after few hours found out that the "R&D" is the problem..
The '&' did not allow my usercontrol items to be enabled in my toolbox.



回答18:

I fell into this trap just a couple of hours ago.
I've got a .NET 2.0 Windows Application project with some custom UserControls; it worked fine. So I decided to order my files in subfolders, to make my project a little bit cleaner.
After that, Visual Studio 2010 designer stopped loading my forms, and ToolBox won't show my controls anymore.
I freaked out, moving back source files in project root, resetting ToolBox, but nothing seemed to work. After that, I remembered I used ReSharper "Remove Unused References", so I tried to put back unused reference, in particular System.Data: problem solved! :O I can't say you why, but this worked for me.
Hope my experience can help someone else. :)
Bye, Nando



回答19:

If you still can't find why your vstudio toolbox is not populated with your usercontrols. Then you can debug vstudio with another visual studio. Here you can find how.



回答20:

If you've tried following all the other answers and it still doesn't work, the following fixed it for me:

  • Right click on your project containing your control.
  • Select Properties.
  • In the Build settings category, make sure Register for COM interop is checked.


回答21:

In my case the reason of error was an excess length of a path to my control's .dll. I had shrunk it a bit and everything became working fine.

Also I found out that the special symbols (e.g. I used #) in a path affect a control display.

Hope it solves the issue.



回答22:

In your UserControl, make sure that New is declared Public. It won't show up if it's just declared Sub New (which defaults to Friend).



回答23:

Symptom 1: The Design Views for Form, UserControl, & Component were FAILING!

  • My Form design view was failing w/ the msg "can not find 'User Control'."
  • If I could get the Form design view to work it was very unstable & corrupted all to hell w/ any change.

Symptom 2: The UserControl & Component in the Toolbox

  • Were grayed out in the Toolbox & showed a garbled name
  • "Choose item" in the Toolbox context menu showed garbled name & no namespace

Solution: Set scope to Public in the vb behind UserControl, & Component



回答24:

Check project path, avoid & , # etc.

I've moved my solution from drive:\work\c#\folder\ to drive:\work\folder and it solved the problem.



回答25:

I was trying to build a x64 only application, so my platform target was set to x64 of course.

However, even in 2016, Visual Studio (devenv.exe) is still a 32 bit process and it cannot load 64 bit assemblies. To check the bitness of your Visual Studio, open Task Manager and check for *32 at the name of the process.

Workaround to see the Controls in the toolbox: set the platform target to Any CPU in the project settings. Do that for Debug and Release build, if necessary.



回答26:

When I tried to add my UserControl to the toolbox (right click toolbox, choose items, select my DLL) it would display a message saying there were no controls in my DLL.

Anyway, the problem was solved, by trying to create a form in my DLL in VS and adding the UserControl. An error message displayed saying there was no default constructor

public UserControl() {
...
}

The designer needs this because it can't know what valid arguments are. Once I added a blank constructor to the UserControl it was added to the toolbox without issue.



回答27:

As mentioned here you should tell the visual studio to load your usercontrol in toolbox.

[ToolboxItem(true)]
public class PanelTitle : LabelControl  {
// Whatever code to override LabelControl here...
}


回答28:

Before trying to add a custom control, make sure that it is compatible with the .Net version of your current project.

I was trying to add a custom control to toolbox for a project for Windows CE 5.0 and .Net Compact v3.5 on VS2008. I did all those previous suggestions but nothing worked. The control shows up when "show all" is checked but it is grayed , even in "Choose Items" menu is checked.

I created a new project with .Net v2.0 and it worked perfectly.



回答29:

hello its answer Ctrl+Alt+X take this and later u have TOOLBOX :)