Hi Cpnet,
As for the adding dependent references and initializing "null" statement
problem you mentioned, here are my suggestions:
1. The VS.NET IDE will compare the value you set for the declaration of the
member field with its default value , if they're the same ,then it won't
add the null assignment in the initalizeComponent method. For example, in
the following component class, it contains a member
public class SimpleComponent : System.ComponentModel.Component
{
private ClassLib.UserName _userName = null;
[Browsable(true),
DefaultValue(null)]
public ClassLib.UserName UserName
{
.....
}
.............
}
we set the initialize value and the "DefaultValue" as the same value "null"
so that when we add the component onto our form, it won't repeat the null
assignment.
2. As for the denpendent assemblies reference, it is the normal behavior
that IDE will only add the component's main assembly. And if we need to let
the IDE auto reference all the denpendences for user when drag the
compoenent from toolbox onto form, we have to provide an additional class
which derived from the System.Drawing.Design.ToolboxItem class and override
its "CreateComponentsCore" function.
This class will do the operations needed when we drag a component onto a
form( do them in the "CreateComponentCore" function), and we need to apply
it to the component class via the "ToolboxItem" attribute , for example:
===================================
[ToolboxItem(typeof(ComponentLib.SimpleComponentToolboxItem))]
public class SimpleComponent : System.ComponentModel.Component
{
....
}
[Serializable]
public class SimpleComponentToolboxItem : System.Drawing.Design.ToolboxItem
{
....
}
And here is a complete demo I've used to test, you may have a look for
detailed reference:
=========ClassLib-------a separate class assembly=============
namespace ClassLib
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class UserName
{
private string _firstName = string.Empty;
private string _lastName = string.Empty;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}
public UserName()
{
}
public UserName(string firstName, string lastName)
{
_firstName = firstName;
_lastName = lastName;
}
}
}
===================================
===========the component assemnbly================
namespace ComponentLib
{
/// <summary>
/// Summary description for SimpleComponent.
/// </summary>
///
[ToolboxItem(typeof(ComponentLib.SimpleComponentToolboxItem))]
public class SimpleComponent : System.ComponentModel.Component
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private ClassLib.UserName _userName = null;
[Browsable(true),
DefaultValue(null)]
public ClassLib.UserName UserName
{
get
{
return _userName;
}
set
{
_userName = value;
}
}
public SimpleComponent(System.ComponentModel.IContainer container)
{
///
/// Required for Windows.Forms Class Composition Designer support
///
container.Add(this);
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
public SimpleComponent()
{
///
/// Required for Windows.Forms Class Composition Designer support
///
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
[Serializable]
public class SimpleComponentToolboxItem : System.Drawing.Design.ToolboxItem
{
public SimpleComponentToolboxItem() : base() {}
private SimpleComponentToolboxItem(SerializationInfo info,
StreamingContext context)
: base(typeof(SimpleComponent))
{
Deserialize(info, context);
}
protected override IComponent[] CreateComponentsCore(IDesignerHost host)
{
ITypeResolutionService service1;
Assembly assembly1,assembly2;
IComponent component1;
IComponent[] componentArray1;
IContainer container1 = host.Container;
service1 = ((ITypeResolutionService)
host.GetService(typeof(ITypeResolutionService)));
assembly1 = typeof(ClassLib.UserName).Module.Assembly;
assembly2 = typeof(ComponentLib.SimpleComponent).Module.Assembly;
service1.ReferenceAssembly(assembly1.GetName());
service1.ReferenceAssembly(assembly2.GetName());
component1 = new ComponentLib.SimpleComponent();
container1.Add(component1);
componentArray1 = new IComponent[]{component1};
return componentArray1;
}
}
}
==================================================
Then, you can try using the component class in a winform or webform project
and try to add it by dragging from toolbox. Both the "UserName"'s assembly
and the component's assembly will be added in the reference list.
You should pay attention to the "SimpleComponentToolboxItem's
CreateComponentsCore method where I put the referencing assembly code
there. If you have anything unclear, please feel free to post here.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx