using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; public class wforms : Form // full name: System.Windows.Forms.Form { public Button button1; public wforms() { button1 = new Button(); button1.Size= new Size(180,30); button1.Location = new Point(50,60); button1.Text = "Don't Click Me"; this.Controls.Add(button1); this.Size = new Size(300,200); // add two event handlers as observers to button button1 button1.Click += new EventHandler(button1handler); button1.Click += new EventHandler((s,e)=>{Console.WriteLine(s+":"+e);}); }//constructor static string[] msg = {"I Said Don't Click Me!","I'M WARNING YOU!", "I'M GETTING ANGRY!","STOP IT NOW!!"}; int clickcnt1 = 0; void button1handler(object snd, EventArgs e) // sender and args { button1.Text = msg[ (clickcnt1++/3)%4 ]; } public static void Main() { Application.EnableVisualStyles(); Application.Run(new wforms()); } }//wforms /* Note the use in this program of several C# language/oop features: 1. Many attributes like .Text, .Size, are not variables but "properties", and = calls set method. 2. Operator overloading of "+" and "+=" possible 3. Event Handlers are delegates 4. Type Inference on the parameters (s,e) in the second delegate/lambda term. 5. Inheritance (from superclass Form), means further inheritance requires emulation. 6. The Observer Pattern: each event handler is an *observer* of the button, and are *updated* when the button is clicked, thus dynamic dispatch used. */