Mega Code Archive

 
Categories / C# / Event
 

Get Event sender in event handler

using System; using System.Drawing; using System.Windows.Forms;     class TwoButtons: Form {      readonly Button btnLarger, btnSmaller;      readonly int    cxBtn, cyBtn, dxBtn;          public static void Main()      {           Application.Run(new TwoButtons());      }      public TwoButtons()      {           ResizeRedraw = true;               cxBtn = 200;           cyBtn = 100;           dxBtn = 12;               btnLarger = new Button();           btnLarger.Parent = this;           btnLarger.Text   = "&Larger";           btnLarger.Size   = new Size(cxBtn, cyBtn);           btnLarger.Click += new EventHandler(ButtonOnClick);               btnSmaller = new Button();           btnSmaller.Parent = this;           btnSmaller.Text   = "&Smaller";           btnSmaller.Size   = new Size(cxBtn, cyBtn);           btnSmaller.Click += new EventHandler(ButtonOnClick);               OnResize(EventArgs.Empty);      }      protected override void OnResize(EventArgs ea)      {           base.OnResize(ea);               btnLarger.Location =                          new Point(ClientSize.Width / 2 - cxBtn - dxBtn / 2,                                   (ClientSize.Height - cyBtn) / 2);           btnSmaller.Location =                          new Point(ClientSize.Width / 2 + dxBtn / 2,                                   (ClientSize.Height - cyBtn) / 2);      }      void ButtonOnClick(object obj, EventArgs ea)      {           Button btn = (Button) obj;               if (btn == btnLarger)           {                Left   -= (int)(0.1 * Width);                Top    -= (int)(0.1 * Height);                Width  += (int)(0.1 * Width);                Height += (int)(0.1 * Height);           }           else           {                Left   += (int)(Width  / 22f);                Top    += (int)(Height / 22f);                Width  -= (int)(Width  / 11f);                Height -= (int)(Height / 11f);           }      } }