C# 如何将我的新用户控件添加到工具箱或新 Winform?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8931328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:16:46  来源:igfitidea点击:

How do I add my new User Control to the Toolbox or a new Winform?

c#winformsvisual-studio-2008user-controls

提问by Robert Harvey

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 Controland dragged some controls onto the new User Control.

我有一个现有的库(不是 Winforms 应用程序),它为真正的 Windows 应用程序提供了一些 Winforms。在这个库中,我想创建一个用户控件来将一些控件组合在一起。为此,我右键单击“添加”“用户控件”并将一些控件拖到新的“用户控件”上。

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

到现在为止还挺好。用户控件甚至具有必要的用户控件图标。但是将新的用户控件从解决方案资源管理器拖到一个新的空白 Winform 不起作用(我得到一个带有一条线的圆圈),并且将它拖到工具箱也不起作用(即使我得到一个 + 号当我将它拖到工具箱上时)。

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

是否有某种 XML 魔法或我缺少的其他东西来完成这项工作?



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.

注意:我在 Visual Studio 2008 中遇到了一些问题,我按照可以在此处找到的解决方法设法解决了这些问题。现在,只需重新构建项目,我就可以将添加到现有项目中的用户控件添加到工具箱中。

采纳答案by dknaack

Assuming I understand what you mean:

假设我明白你的意思:

  1. If your UserControlis 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 UserControlis part of your project you only need to build the entire solution. After that, your UserControlshould appear in the toolbox.

  1. 如果您UserControl在库中,您可以使用以下命令将其添加到您的工具箱中

    工具箱 -> 右键单击​​ ->选择项目->Browse

    使用UserControl.

  2. 如果它UserControl是您项目的一部分,您只需要构建整个解决方案。之后,您UserControl应该出现在工具箱中。

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

通常,无法从解决方案资源管理器添加控件,只能从工具箱添加。

Enter image description here

在此处输入图片说明

回答by GravityWell

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."

获得此错误的一种方法是在项目设置为编译为 x64 时尝试向窗体添加用户控件。Visual Studio 抛出无用的信息:“加载工具箱项失败。它将从工具箱中删除。

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

解决方法是使用“任何 CPU”进行设计并根据需要编译为 x64。

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

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

回答by Wincent

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

一个用户控件不能应用于它自己。所以打开另一个winform,它就会出现在工具箱中。

回答by Luke

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.

我发现用户控件可以存在于同一个项目中。
正如其他人提到的,AutoToolboxPopulate 必须设置为 True。
创建所需的用户控件。
选择构建解决方案。
如果新用户控件未显示在工具箱中,请关闭/打开 Visual Studio。
如果用户控件仍未显示在工具箱中,请右键单击工具箱并选择重置工具箱。然后选择构建解决方案。如果它们仍然不存在,请重新启动 Visual Studio。
构建解决方案时不能有任何构建错误,否则新的工具箱项将不会添加到工具箱中。