■Win Formsのコード例(C#で記述したものから一部分のみを抜粋)

(赤字はコードに対するコメント)

 1: public class Form1 : System.WinForms.Form ←.NET Frameworkのクラスより継承
 2: {
 3:     private System.ComponentModel.Container components;
 4:     private System.WinForms.Button button2;
 5:     private System.WinForms.Button button1;
 6:
 7:     public Form1() ←コンストラクタ
 8:     {
 9:         InitializeComponent();
10:         // TODO: Add any constructor code after InitializeComponent call
11:     }
12:
13:     public override void Dispose() ←デストラクタ
14:     {
15:         base.Dispose();
16:         components.Dispose();
17:     }
18:
19:     private void InitializeComponent()
20:         {
21:                 this.components = new System.ComponentModel.Container();
22:                 this.button2 = new System.WinForms.Button();
23:                 this.button1 = new System.WinForms.Button();
24:
25:                 button2.Location = new System.Drawing.Point(168, 344);
26:                 button2.Size = new System.Drawing.Size(128, 24);
27:                 button2.TabIndex = 1;
28:                 button2.Text = "button2";
29:                 button2.AddOnClick(new System.EventHandler(button2_Click)); ←ボタンのイベント・ハンドラを設定
30:
31:                 button1.Location = new System.Drawing.Point(304, 344);
32:                 button1.Size = new System.Drawing.Size(144, 24);
33:                 button1.TabIndex = 0;
34:                 button1.Text = "button1";
35:
36:                 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
37:                 this.Text = "Form1";
38:                 //@design this.TrayLargeIcon = true;
39:                 //@design this.TrayHeight = 0;
40:                 this.ClientSize = new System.Drawing.Size(448, 373);
41:
42:                 this.Controls.Add(button2);
43:                 this.Controls.Add(button1);
44:
45:         }
46:         protected void button2_Click(object sender, System.EventArgs e) ←ボタンが押されたら呼び出されるメソッド
47:         {
48:
49:         }
50:
51:     /*
52:      * The main entry point for the application.
53:      */
54:     public static void Main(string[] args) ←Mainメソッドより実行が始まる
55:     {
56:         Application.Run(new Form1());
57:     }
58: }