Mega Code Archive

 
Categories / C# Tutorial / GUI Windows Forms
 

Use delegate to add an event handler to a Button

using System; using System.Drawing; using System.Windows.Forms; public class ButtonEventDelegate : System.Windows.Forms.Form {   private Button btn;   public ButtonEventDelegate()   {         Text = "Hello World";     btn = new Button();     btn.Location = new Point(50,50);     btn.Text = "Goodbye";     btn.Click += new System.EventHandler(btn_Click);     Controls.Add(btn);   }   static void Main()    {     Application.Run(new ButtonEventDelegate());   }   private void btn_Click(object sender, EventArgs e)   {     Application.Exit();   } }