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

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

20

21
 //This code will add the following line to the "InitializeMethod" method
   //This code will add the following line to the "InitializeMethod" method22
 // this.Load += new System.EventHandler(this.Form1_Load);
   // this.Load += new System.EventHandler(this.Form1_Load);23
 //---------------------------------------------------------------------------
   //---------------------------------------------------------------------------24
 member = new CodeMemberMethod();
   member = new CodeMemberMethod();25
 foreach (CodeTypeMember typememb in typedecl.Members)
   foreach (CodeTypeMember typememb in typedecl.Members)26
 {
   {27
 if (typememb.Name == "InitializeComponent")
       if (typememb.Name == "InitializeComponent")28
 { member = (CodeMemberMethod)typememb; }
       { member = (CodeMemberMethod)typememb; }29
 }
   }30
 CodeDelegateCreateExpression createDelegate1;
   CodeDelegateCreateExpression createDelegate1;31
 createDelegate1 = new CodeDelegateCreateExpression(new CodeTypeReference("System.EventHandler"), new CodeThisReferenceExpression(), "Form1_Load");
   createDelegate1 = new CodeDelegateCreateExpression(new CodeTypeReference("System.EventHandler"), new CodeThisReferenceExpression(), "Form1_Load");32
 CodeAttachEventStatement attach = new CodeAttachEventStatement(new CodeThisReferenceExpression(), "Load", createDelegate1);
   CodeAttachEventStatement attach = new CodeAttachEventStatement(new CodeThisReferenceExpression(), "Load", createDelegate1);33
 member.Statements.Add(attach);
   member.Statements.Add(attach);34
 typedecl.Members.Add(member);
   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,
   //Add and remove a label because otherwise the code to add the method seems to stay "inactive,39
 //while in this way it works
   //while in this way it works40
 //---------------------------------------------------------------------------
   //---------------------------------------------------------------------------41
 Label lbl = (Label)host.CreateComponent(typeof(Label));
   Label lbl = (Label)host.CreateComponent(typeof(Label));42
 host.DestroyComponent(lbl);
   host.DestroyComponent(lbl);43
 //---------------------------------------------------------------------------
   //---------------------------------------------------------------------------44
 }
} 
                    
                     
                    
                 
                    
                 
 
        

 
    
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号