I have a user control with in a control library:
[Designer(typeof(MyControlDesigner))]
class MyCoontrol: UserControl
{
...
}
class myControlDesigner: ScrollableControlDesigner
{
...
public override DesignerActionListCollection ActionLists
{
get
{
if (null == actionLists)
{
actionLists = new DesignerActionListCollection();
actionLists.Add(new MyActionList(this.Component));
}
return actionLists;
}
}
public class MyActionList : System.ComponentModel.Design.DesignerActionList
{
public DataActionList(IComponent component)
: base(component)
{
}
public override DesignerActionItemCollection GetSortedActionItems()
{
DesignerActionItemCollection items = new DesignerActionItemCollection();
items.Add(new DesignerActionMethodItem(this, "AddControl", "Add New Control", false));
return items;
}
void AddControl()
{
MessageBox.Show("Try to add an Button");
IDesignerHost host = this.Component.Site.GetService(typeof(IDesignerHost)) as IDesignerHost;
IDesigner designer = host.GetDesigner(this.Component);
System.Windows.Forms.Button ctl = host.CreateComponent(typeof(System.Windows.Forms.Button)) as System.Windows.Forms.Button;
ctl.Size = new Size(100, 20);
ctl.Location = new Point(20, 30);
ctl.Text = "New Added button";
(this.Compenent.Site.GetService(typeof(DesignerActionUIService)) as DesignerActionUIService).Refresh(this.Compenent);
}
}
}
Now I added this library to a Windows Forms project, and in the design time, when I excute the AddControl method, there is no Butteon displayed on the design surface of MyControl. Looking at the Form's method:
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
...
// button1
//
this.button1.Location = new System.Drawing.Point(20, 30);
this.button1.Name = "button3";
this.button1.Size = new System.Drawing.Size(100, 20);
this.button1.TabIndex = 0;
this.button1.Text = "New Added button";
...
}
The button is generated, but itwasn'tadded to MyControl's control collection (MyContro.Controls).
Did I miss anything?
Thanks
ZC