Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Create Control Programatically in Design Time
 

Create Control Programatically in Design Time

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

ZCH  Wednesday, April 22, 2009 3:03 AM
Hi, Kira


Sorry. I think I solved the issue. In MyActionList.AddControl method, I need to add the created button to the control collections explicitly: MyControl.Controls.Add(button).



Thanks
ZC


  • Marked As Answer byZCH Thursday, April 23, 2009 11:10 AM
  •  
ZCH  Thursday, April 23, 2009 11:10 AM

Hi ZCH,

What does "in the design time, when I excute the AddControl method" mean? How did you call a method in design time? Am I misunderstand your meaning?

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Thursday, April 23, 2009 9:53 AM
Hi, Kira


Sorry. I think I solved the issue. In MyActionList.AddControl method, I need to add the created button to the control collections explicitly: MyControl.Controls.Add(button).



Thanks
ZC


  • Marked As Answer byZCH Thursday, April 23, 2009 11:10 AM
  •  
ZCH  Thursday, April 23, 2009 11:10 AM

You can use google to search for other answers

Custom Search

More Threads

• Datagrid Combobox Column
• Iterating DesignerSerializationVisibility.Content collections?
• Passing value from child from to parent form
• Implementing IButtonControl
• Why can't I change a simple name
• Create Popup Alert
• UserControl custom property and UndoEngine
• User Control child controls move around
• How to let application override event handler in User Control?
• Track the Shift Tab key in Textbox