User Controls not showing up in the toolbox

2019-01-21 21:19发布

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?

29条回答
该账号已被封号
2楼-- · 2019-01-21 21:37

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...
}
查看更多
Summer. ? 凉城
3楼-- · 2019-01-21 21:38

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.

查看更多
SAY GOODBYE
4楼-- · 2019-01-21 21:38

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.

查看更多
We Are One
5楼-- · 2019-01-21 21:38

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.

查看更多
太酷不给撩
6楼-- · 2019-01-21 21:39

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. :)

查看更多
forever°为你锁心
7楼-- · 2019-01-21 21:39

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).
查看更多
登录 后发表回答