.NET VS2005 WinForms:如何将用户控件放到表单上?

时间:2021-11-24 07:22:00

i've written a UserControl descendant that is in an assembly dll.

我写了一个装配dll中的UserControl后代。

How do i drop the control on a form?

如何删除表单上的控件?

namespace *Example
{
   public partial class MonthViewCalendar : UserControl
   {
      ...
   }
}

i've added a reference to the assembly under the References node in the Solution Explorer, but no new control has appeared in my Toolbox.

我在解决方案资源管理器中的“引用”节点下添加了对程序集的引用,但我的工具箱中没有出现新的控件。

How do i make the control appear in the Toolbox so i can drop it on a form?

如何使控件出现在工具箱中,以便我可以将其放在表单上?


Update 1:

i tried building the assembly while the Visual Studio option:

我尝试在Visual Studio选项中构建程序集:

Tools-->Options...-->Windows Forms Designer-->AutoToolboxPopulate = true

工具 - >选项...--> Windows窗体设计器 - > AutoToolboxPopulate = true

The control didn't appear when in the toolbox in a new solution.

在新解决方案中的工具箱中未显示该控件。

Note: i somehow mistakenly wrote "...that is not in an assembly dll...". i don't know how i managed to write that, when it specifically is in an assembly dll. Controls have magically appeared when they're in the same project, but not now that it's a different project/solution.

注意:我不知何故错误地写了“......那不是装配dll ......”。我不知道我是怎么设法写的,当它特别是在一个程序集dll中。当他们在同一个项目中时,控件已经神奇地出现了,但现在不是它是一个不同的项目/解决方案。


Update 2: Answer

更新2:答案

  1. Right-click the Toolbox
  2. 右键单击“工具箱”

  3. Select Choose Items...
  4. 选择选择项目...

  5. .NET Framework Components tab
  6. “.NET Framework组件”选项卡

  7. Select Browse...
  8. Browse to the assembly dll file that contains the control and select Open

    浏览到包含该控件的程序集dll文件,然后选择“打开”

    Note: Controls in the assembly will silently be added to the list of .NET Framework Components.

    注意:程序集中的控件将以静默方式添加到.NET Framework组件列表中。

  9. Check each of the controls you wish to appear in the toolbox
  10. 检查您希望在工具箱中显示的每个控件

  11. Select OK

5 个解决方案

#1


4  

Normally, when you build your project, your user control will appear in your toolbox at the top. Normally, you will see a new pane with each of your Assemblies and the controls in there.

通常,在构建项目时,用户控件将显示在顶部的工具箱中。通常,您将看到一个新窗格,其中包含每个程序集和控件。

If that doesn't happen, you can also add your control by right clicking on the toolbox, selecting Choose Items, then under .NET Framework Components browsing for your assembly, adding it, then make sure your control is checked.

如果没有发生这种情况,您还可以通过右键单击工具箱,选择“选择项目”,然后在“.NET Framework组件”浏览组件,添加它,然后确保选中控件来添加控件。

#2


1  

What I notice is that User Controls and Components are only automatically added to the Toolbox by vs2005 when your project (containing the controls/components) is located in the same folder as your solution. When this project is in a subfolder vs2005 won't add the controls and components in the Toolbox.

我注意到,当您的项目(包含控件/组件)与您的解决方案位于同一文件夹中时,用户控件和组件仅由vs2005自动添加到工具箱中。当此项目位于子文件夹vs2005中时,不会在工具箱中添加控件和组件。

#3


0  

I stumbled upon some problems with this. In the end, just rebuild and re-reference will work. I had preferred to inherit from UserControl. It made my life simpler ;)

我偶然发现了一些问题。最后,只需重建和重新引用即可。我更喜欢从UserControl继承。这让我的生活更简单;)

If for example you want to create a "rounded border" label, do something like this:

例如,如果要创建“圆角边框”标签,请执行以下操作:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace YourUIControls
{
    [DefaultProperty("TextString")]
    [DefaultEvent("TextClick")]
    public partial class RoundedLabel : UserControl
    {
         public RoundedLabel()
         {
             InitializeComponent();
         }
         protected override void OnPaint(PaintEventArgs e)
         {
             //Draw your label here…
         }
     }
}

Compile and add a reference to the output. You'll be able to drag that to the Toolbox and later to the Designer.

编译并添加对输出的引用。您可以将其拖到工具箱中,然后再拖到Designer中。

#4


0  

You need to build the project containing the control you've created and make sure your options are set for the Toolbox to rebuild. If you haven't changed it from defaults, it should work; otherwise, go to Tools-->Options... and select the Windows Forms Designer group. Make sure the AutoToolboxPopulate option is set to true.

您需要构建包含您创建的控件的项目,并确保为工具箱重新设置了选项。如果您没有从默认值更改它,它应该工作;否则,请转到“工具” - >“选项...”,然后选择“Windows窗体设计器”组。确保AutoToolboxPopulate选项设置为true。

You don't need the ToolboxItemAttribute for it to work. If the providing assembly is in the same solution as the consuming assembly, it should appear in the toolbox. If the providing assembly is not part of the solution, you can manually add the assembly to the toolbox by selecting **Choose items...* from the toolbox context menu and adding your assembly directly. If you want the toolbox to automatically pick them up, you will need to use the ToolboxItemAttribute.

您不需要ToolboxItemAttribute就可以使用它。如果提供组件与消耗组件位于同一解决方案中,它应出现在工具箱中。如果提供程序集不是解决方案的一部分,则可以通过从工具箱上下文菜单中选择**选择项目... *并直接添加程序集,手动将程序集添加到工具箱。如果您希望工具箱自动拾取它们,则需要使用ToolboxItemAttribute。

#5


-1  

Add the ToolboxAttribute to your class.

将ToolboxAttribute添加到您的类。

#1


4  

Normally, when you build your project, your user control will appear in your toolbox at the top. Normally, you will see a new pane with each of your Assemblies and the controls in there.

通常,在构建项目时,用户控件将显示在顶部的工具箱中。通常,您将看到一个新窗格,其中包含每个程序集和控件。

If that doesn't happen, you can also add your control by right clicking on the toolbox, selecting Choose Items, then under .NET Framework Components browsing for your assembly, adding it, then make sure your control is checked.

如果没有发生这种情况,您还可以通过右键单击工具箱,选择“选择项目”,然后在“.NET Framework组件”浏览组件,添加它,然后确保选中控件来添加控件。

#2


1  

What I notice is that User Controls and Components are only automatically added to the Toolbox by vs2005 when your project (containing the controls/components) is located in the same folder as your solution. When this project is in a subfolder vs2005 won't add the controls and components in the Toolbox.

我注意到,当您的项目(包含控件/组件)与您的解决方案位于同一文件夹中时,用户控件和组件仅由vs2005自动添加到工具箱中。当此项目位于子文件夹vs2005中时,不会在工具箱中添加控件和组件。

#3


0  

I stumbled upon some problems with this. In the end, just rebuild and re-reference will work. I had preferred to inherit from UserControl. It made my life simpler ;)

我偶然发现了一些问题。最后,只需重建和重新引用即可。我更喜欢从UserControl继承。这让我的生活更简单;)

If for example you want to create a "rounded border" label, do something like this:

例如,如果要创建“圆角边框”标签,请执行以下操作:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace YourUIControls
{
    [DefaultProperty("TextString")]
    [DefaultEvent("TextClick")]
    public partial class RoundedLabel : UserControl
    {
         public RoundedLabel()
         {
             InitializeComponent();
         }
         protected override void OnPaint(PaintEventArgs e)
         {
             //Draw your label here…
         }
     }
}

Compile and add a reference to the output. You'll be able to drag that to the Toolbox and later to the Designer.

编译并添加对输出的引用。您可以将其拖到工具箱中,然后再拖到Designer中。

#4


0  

You need to build the project containing the control you've created and make sure your options are set for the Toolbox to rebuild. If you haven't changed it from defaults, it should work; otherwise, go to Tools-->Options... and select the Windows Forms Designer group. Make sure the AutoToolboxPopulate option is set to true.

您需要构建包含您创建的控件的项目,并确保为工具箱重新设置了选项。如果您没有从默认值更改它,它应该工作;否则,请转到“工具” - >“选项...”,然后选择“Windows窗体设计器”组。确保AutoToolboxPopulate选项设置为true。

You don't need the ToolboxItemAttribute for it to work. If the providing assembly is in the same solution as the consuming assembly, it should appear in the toolbox. If the providing assembly is not part of the solution, you can manually add the assembly to the toolbox by selecting **Choose items...* from the toolbox context menu and adding your assembly directly. If you want the toolbox to automatically pick them up, you will need to use the ToolboxItemAttribute.

您不需要ToolboxItemAttribute就可以使用它。如果提供组件与消耗组件位于同一解决方案中,它应出现在工具箱中。如果提供程序集不是解决方案的一部分,则可以通过从工具箱上下文菜单中选择**选择项目... *并直接添加程序集,手动将程序集添加到工具箱。如果您希望工具箱自动拾取它们,则需要使用ToolboxItemAttribute。

#5


-1  

Add the ToolboxAttribute to your class.

将ToolboxAttribute添加到您的类。