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条回答
男人必须洒脱
2楼-- · 2019-01-07 09:03

Right-click on toolbar then click on "choose item" in context menu. A dialog with registered components pops up. in this dialog click "Browse" to select your assembly with the usercontrol you want to use.

PS. This assembly should be registered before.

查看更多
成全新的幸福
3楼-- · 2019-01-07 09:04

The issue with my designer was 32 vs 64 bit issue. I could add the control to tool box after following the instructions in Cannot add Controls from 64-bit Assemblies to the Toolbox or Use in Designers Within the Visual Studio IDE MS KB article.

查看更多
地球回转人心会变
4楼-- · 2019-01-07 09:09

As far as I know you can drag&drop your usercontrol in your current project to the toolbox. The problem is when you choose a different project the user control is gone. So I think you have to build a separate control library project:

In your solution, add a new project, however as a project template choose "Class Library". In THAT project, add a user control, and do everything you need to do. Once that's done, build it, and you'll have yourself a dll file (it can easily be found in the projects bin/debug folder.)

You can add this dll to your toolbox (right click add...) and it will stay there in all projects!

查看更多
别忘想泡老子
5楼-- · 2019-01-07 09:10

Basic qustion if you are using generics in your base control. If yes:

lets say we have control:

public class MyComboDropDown : ComboDropDownComon<MyType>
{
    public MyComboDropDown() { }
}

MyComboDropDown will not allow to open designer on it and will be not shown in Toolbox. Why? Because base control is not already compiled - when MyComboDropDown is complied. You can modify to this:

public class MyComboDropDown : MyComboDropDownBase
{
    public MyComboDropDown() { }
}

public class MyComboDropDownBase : ComboDropDownComon<MyType>
{

}

Than after rebuild, and reset toolbox it should be able to see MyComboDropDown in designer and also in Toolbox

查看更多
迷人小祖宗
6楼-- · 2019-01-07 09:12

Using VS 2010:

Let's say you have a Windows.Forms project. You add a UserControl (say MyControl) to the project, and design it all up. Now you want to add it to your toolbox.

As soon as the project is successfully built once, it will appear in your Framework Components. Right click the Toolbox to get the context menu, select "Choose Items...", and browse to the name of your control (MyControl) under the ".NET Framework Components" tab.

Advantage over using dlls: you can edit the controls in the same project as your form, and the form will build with the new controls. However, the control will only be avilable to this project.

Note: If the control has build errors, resolve them before moving on to the containing forms, or the designer has a heart attack.

查看更多
登录 后发表回答