Hide custom control in VS.NET toolbox


G

Guest

I have created a set of custom controls, which all inherit from one base
control. I am using these controls within the same project, so they I would
like to hide the base control so that it does not show up in "My User
Controls". Is there a class attribute I can set to accomplish this?
 
Ad

Advertisements

G

Guest

Hello Tim,

Thanks for the suggestions. I am using Visual Studio .NET 2003, and my
controls (base and derived controls) are in an assembly called MI.UI.dll. I
have tried the following with my base control:

<System.ComponentModel.ToolboxItem(False)> _
Public Class MICompositeControlBase
Inherits System.Windows.Forms.UserControl
....

However, this base control still shows up in the Toolbox (when I reference
the MI.UI project directly) along with the other controls, or NOTHING shows
up in the Toolbox (when I reference the compiled MI.UI.dll assembly). This
appears to be a problem for other people, as described here:

http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet.buildingcontrols/browse_frm/thread/89923daf965d9b71/c03a3a4b042d9a0e?q=toolboxitem&_done=/groups?q=toolboxitem

and here:

http://www.lemanix.com/nick/archive/2005/02/08/1656.aspx

I have also tried making the base class abstract (MustInherit in VB.NET),
but this does not hide the base control from the Toolbox either. In
addition, this has the unfortunate sideeffect that I cannot any longer open
the designer for the derived controls, because of this message:

An error occurred while loading the document. Fix the error, and then try
loading the document again. The error message follows: The designer must
create an instance of type 'MI.UI.MICompositeControlBase' but it cannot
because the type is declared as abstract.

- David McClelland
 
Ad

Advertisements

T

Tim Wilson

The best thing to do would be to make the base control MustInherit since it
doesn't sound like you want to be able to make instances of this control.
However, you would not be able to visually design the derived controls since
the designer needs to create an instance of the base class and it cannot
since it is "MustInherit", as was indicated by the error message. So you'll
need to either not visually design the controls or you'll need to use the
ToolboxItemAttribute. It looks like that ToolboxItemAttribute is getting
inherited on the derived controls. What you can do is indicate that the base
control should not show in the Toolbox and then indicate on derived controls
that they should. Then when you compile the control assembly and add it to
the Toolbox only the derived controls will show.

<System.ComponentModel.ToolboxItem(False)> _
Public Class BaseControl
Inherits System.Windows.Forms.UserControl

...

End Class

<System.ComponentModel.ToolboxItem(True)> _
Public Class DerivedControl
Inherits BaseControl

...

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top