1
private void AddCode(Form form)
2
{
3
IDesignerHost host = null;
4
host = (IDesignerHost)form.Site.GetService(typeof(IDesignerHost));
5
6
//Add method "Form1_Load" on Form1
7
//---------------------------------------------------------------------------
8
CodeMemberMethod member = new CodeMemberMethod();
9
member.Name = "Form1_Load";
10
member.Parameters.Add(new CodeParameterDeclarationExpression("System.Object", "sender"));
11
member.Parameters.Add(new CodeParameterDeclarationExpression("System.EventArgs", "e"));
12
CodeSnippetExpression sn;
13
sn = new CodeSnippetExpression("MessageBox.Show(\"Hello world\")");
14
member.Statements.Add(sn);
15
member.Attributes = MemberAttributes.Private;
16
CodeTypeDeclaration typedecl = (CodeTypeDeclaration)form.Site.GetService(typeof(CodeTypeDeclaration));
17
typedecl.Members.Add(member);
18
//---------------------------------------------------------------------------
19
20
21
//This code will add the following line to the "InitializeMethod" method
22
// this.Load += new System.EventHandler(this.Form1_Load);
23
//---------------------------------------------------------------------------
24
member = new CodeMemberMethod();
25
foreach (CodeTypeMember typememb in typedecl.Members)
26
{
27
if (typememb.Name == "InitializeComponent")
28
{ member = (CodeMemberMethod)typememb; }
29
}
30
CodeDelegateCreateExpression createDelegate1;
31
createDelegate1 = new CodeDelegateCreateExpression(new CodeTypeReference("System.EventHandler"), new CodeThisReferenceExpression(), "Form1_Load");
32
CodeAttachEventStatement attach = new CodeAttachEventStatement(new CodeThisReferenceExpression(), "Load", createDelegate1);
33
member.Statements.Add(attach);
34
typedecl.Members.Add(member);
35
//---------------------------------------------------------------------------
36
37
38
//Add and remove a label because otherwise the code to add the method seems to stay "inactive,
39
//while in this way it works
40
//---------------------------------------------------------------------------
41
Label lbl = (Label)host.CreateComponent(typeof(Label));
42
host.DestroyComponent(lbl);
43
//---------------------------------------------------------------------------
44
}
private void AddCode(Form form)2
{3
IDesignerHost host = null;4
host = (IDesignerHost)form.Site.GetService(typeof(IDesignerHost));5

6
//Add method "Form1_Load" on Form17
//---------------------------------------------------------------------------8
CodeMemberMethod member = new CodeMemberMethod();9
member.Name = "Form1_Load";10
member.Parameters.Add(new CodeParameterDeclarationExpression("System.Object", "sender"));11
member.Parameters.Add(new CodeParameterDeclarationExpression("System.EventArgs", "e"));12
CodeSnippetExpression sn;13
sn = new CodeSnippetExpression("MessageBox.Show(\"Hello world\")");14
member.Statements.Add(sn);15
member.Attributes = MemberAttributes.Private;16
CodeTypeDeclaration typedecl = (CodeTypeDeclaration)form.Site.GetService(typeof(CodeTypeDeclaration));17
typedecl.Members.Add(member);18
//---------------------------------------------------------------------------19

20

21
//This code will add the following line to the "InitializeMethod" method22
// this.Load += new System.EventHandler(this.Form1_Load);23
//---------------------------------------------------------------------------24
member = new CodeMemberMethod();25
foreach (CodeTypeMember typememb in typedecl.Members)26
{27
if (typememb.Name == "InitializeComponent")28
{ member = (CodeMemberMethod)typememb; }29
}30
CodeDelegateCreateExpression createDelegate1;31
createDelegate1 = new CodeDelegateCreateExpression(new CodeTypeReference("System.EventHandler"), new CodeThisReferenceExpression(), "Form1_Load");32
CodeAttachEventStatement attach = new CodeAttachEventStatement(new CodeThisReferenceExpression(), "Load", createDelegate1);33
member.Statements.Add(attach);34
typedecl.Members.Add(member);35
//---------------------------------------------------------------------------36

37

38
//Add and remove a label because otherwise the code to add the method seems to stay "inactive,39
//while in this way it works40
//---------------------------------------------------------------------------41
Label lbl = (Label)host.CreateComponent(typeof(Label));42
host.DestroyComponent(lbl);43
//---------------------------------------------------------------------------44
}
