I don't want to worry about it anymore.

Source: Internet
Author: User
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 (), "formateload ");
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}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.