如何将 UserControl 放入 Visual Studio 工具箱

发布于 2024-09-13 19:31:25 字数 143 浏览 9 评论 0原文

我在项目中制作了一个用户控件,构建项目后,我需要将其放入我的工具箱中,并将其用作常用控件。但我不能。 UserControl 在我的项目命名空间中,我尝试在右键菜单中选择Choose Item,但我没有找到添加它的方法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

忘年祭陌 2024-09-20 19:31:25

我在让它们自动添加到工具箱(如 VS2008/2005 中)时遇到问题。

实际上有一个选项可以阻止工具箱自动填充!

转到工具>选项> Windows 窗体设计器 >常规

在列表底部,您会发现“工具箱”>“工具箱”。 AutoToolboxPopulate 在全新安装时默认为 False。将其设置为 true,然后重建您的解决方案。

嘿,很快,解决方案中的用户控件应该自动添加到工具箱中。

您可能还需要重新加载解决方案。

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, the user controls in your solution should be automatically added to the toolbox.

You might have to reload the solution as well.

洋洋洒洒 2024-09-20 19:31:25

右键单击工具栏,然后单击上下文菜单中的“选择项目”。弹出一个包含已注册组件的对话框。在此对话框中单击“浏览”以选择包含要使用的用户控件的程序集。

附言。
该程序集应该之前注册。

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.

悲歌长辞 2024-09-20 19:31:25

我发现用户控件必须有一个无参数构造函数,否则它不会显示在列表中。至少在 vs2005 中是这样。

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.

旧街凉风 2024-09-20 19:31:25

使用 VS 2010:

假设您有一个 Windows.Forms 项目。您将一个 UserControl(例如 MyControl)添加到项目中,并进行全部设计。现在您想将其添加到您的工具箱中。

一旦项目成功构建一次,它将出现在您的框架组件中。右键单击“工具箱”以获取上下文菜单,选择“选择项...”,然后浏览到“.NET Framework 组件”选项卡下的控件名称 (MyControl)。

与使用 dll 相比的优点:您可以在与表单相同的项目中编辑控件,并且表单将使用新控件构建。但是,该控件仅适用于该项目。

注意:如果控件存在构建错误,请在继续处理包含的表单之前解决它们,否则设计人员会心脏病发作。

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.

奢望 2024-09-20 19:31:25

我有许多用户控件,但其中一个控件拒绝在工具箱中显示,即使我重建了解决方案并且在选择项目...对话框中检查了它。

解决方案:

  1. 解决方案资源管理器中,我右键单击有问题的用户控制文件并选择从项目中排除
  2. 重建解决方案
  3. 右键单击用户控件并选择包含在项目中(假设您在解决方案资源管理器中启用了显示所有文件

注意 这还要求您启用 AutoToolboxPopulate 选项。正如@DaveF 的回答所示。

替代解决方案:我不确定这是否有效,而且我无法尝试它,因为我已经解决了我的问题,但如果您从“选择项目...”对话框中取消选中用户控件,请点击好的,然后重新打开它并检查用户控件。这也可能有效。

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.

天荒地未老 2024-09-20 19:31:25

有几种方法。

  1. 在您的原始项目中,选择“文件”|“导出模板”
    然后选择 ItemTemplate 并按照向导进行操作。

  2. 将您的 UserControl 移至单独的 ClassLibrary(并修复命名空间等)。
    从需要它的项目中向类库添加引用。不用关心 GAC 或其他任何东西,只需关心 DLL 文件即可。

我不建议将 UserControl 放入普通的工具箱中,但这是可以做到的。查看@Arseny 的答案

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

为人所爱 2024-09-20 19:31:25

就我而言,我看不到项目中的任何控件。仅当右键单击工具箱并选择“显示全部”时,我才看到它们,但它们已被禁用......

将项目类型从 Windows 应用程序更改为 ClassLibrary 进行了修复。

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.

相权↑美人 2024-09-20 19:31:25

如果您在基本控件中使用泛型,这是基本问题。
如果是:

假设我们有控制权:

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

MyComboDropDown 将不允许在其上打开设计器,并且不会显示在工具箱中。
为什么?因为当编译 MyComboDropDown 时,基本控件尚未编译。
您可以修改为:

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

public class MyComboDropDownBase : ComboDropDownComon<MyType>
{

}

比重建后,并重置工具箱它应该能够在设计器和工具箱中看到 MyComboDropDown

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

墟烟 2024-09-20 19:31:25

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.

蓝眼泪 2024-09-20 19:31:25

重新编译对我来说成功了!

Recompiling did the trick for me!

痴梦一场 2024-09-20 19:31:25

我刚刚在 VS 2022 中遇到了这个问题。可能有一个快速/简单的答案。

我的快速而肮脏的用户控件不会出现在工具箱中(完全重建等)。

我退出解决方案和 VS,重新加载所有内容,重新构建,它出现并工作。

I just had this issue with VS 2022. There may be a quick/easy answer.

My quick and dirty user control would not appear in the toolbox (full rebuild etc.).

I quit the solutiuon and VS, reloaded all, rebuilt and it appeared and worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文