How to put a UserControl into Visual Studio toolBo

2019-01-07 08:11发布

I made a usercontrol in my project, and after building project, I need to put it in my toolbox, and use it as a common control. but i can't. the UserControl is in my project namespace, and I tried Choose Item in right click menu, but I didn't find a way to add it.

11条回答
Anthone
2楼-- · 2019-01-07 08:46

I found that the user control must have a parameterless constructor or it won't show up in the list. at least that was true in vs2005.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-07 08:47

In my case, I couldn't see any of the controls in the project. Only when right clicking on toolBox and selecting "Show All" I saw them, but yet they were disabled...

Changing Project type from Windows application to ClassLibrary made the fix.

查看更多
姐就是有狂的资本
4楼-- · 2019-01-07 08:53

I'm assuming you're using VS2010 (that's what you've tagged the question as) I had problems getting them to add automatically to the toolbox as in VS2008/2005. There's actually an option to stop the toolbox auto populating!

Go to Tools > Options > Windows Forms Designer > General

At the bottom of the list you'll find Toolbox > AutoToolboxPopulate which on a fresh install defaults to False. Set it true and then rebuild your solution.

Hey presto they user controls in you solution should be automatically added to the toolbox. You might have to reload the solution as well.

查看更多
仙女界的扛把子
5楼-- · 2019-01-07 08:58

I had many users controls but one refused to show in the Toolbox, even though I rebuilt the solution and it was checked in the Choose Items... dialog.

Solution:

  1. From Solution Explorer I Right-Clicked the offending user control file and selected Exclude From Project
  2. Rebuild the solution
  3. Right-Click the user control and select Include in Project (assuming you have the Show All Files enabled in the Solution Explorer)

Note this also requires you have the AutoToolboxPopulate option enabled. As @DaveF answer suggests.

Alternate Solution: I'm not sure if this works, and I couldn't try it since I already resolved my issue, but if you unchecked the user control from the Choose Items... dialog, hit OK, then opened it back up and checked the user control. That might also work.

查看更多
beautiful°
6楼-- · 2019-01-07 08:59

There are a couple of ways.

  1. In your original Project, choose File|Export template
    Then select ItemTemplate and follow the wizard.

  2. Move your UserControl to a separate ClassLibrary (and fix namespaces etc).
    Add a ref to the classlibrary from Projects that need it. Don't bother with the GAC or anything, just the DLL file.

I would not advice putting a UserControl in the normal ToolBox, but it can be done. See the answer from @Arseny

查看更多
欢心
7楼-- · 2019-01-07 09:01

Recompiling did the trick for me!

查看更多
登录 后发表回答