views:

396

answers:

4

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

How do i drop the control on a form?

namespace StackOverflowExample
{
   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:

Tools-->Options...-->Windows Forms Designer-->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.


Update 2: Answer

  1. Right-click the Toolbox
  2. Select Choose Items...
  3. .NET Framework Components tab
  4. Select Browse...
  5. Browse to the assembly dll file that contains the control and select Open

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

  6. Check each of the controls you wish to appear in the toolbox
  7. Select OK
A: 

Add the ToolboxAttribute to your class.

Nick Berardi
i cannot find documentation on the ToolboxAttribute on MSDN, the local help, stackoverflow, or google. Can you provide the namespace and constructor/sample usage?
Ian Boyd
A: 

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.

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.

Jeff Yates
He needs the ToolboxAttribute if he wants anybody else to use the control with out having to jump through hoops
Nick Berardi
That option in Visual Studio is enabled while i built the solution.The control does not appear in the toolbox when i create a new WinForms project in a new solution.
Ian Boyd
The edit to the question clarifies that you want this in a new solution. I've edited my response accordingly. Thanks.
Jeff Yates
Can you provide any documentation on this "ToolBoxAttribute"? i cannot find any references for it on MSDN, Google, Stackoverflow, or the online help.
Ian Boyd
My mistake. I assumed Nick's post used the correct type name. The attribute is actually ToolboxItemAttribute. I've udpated my post accordingly.
Jeff Yates
+3  A: 

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.

Rob Prouse
A: 

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

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.

Martín Marconcini
Problem with the phrase "drag that to the Toolbox", is that you have to have something to drag it from.
Ian Boyd