SHARE
TWEET

Handle GUI Keyboard and Mouse events

a guest Mar 6th, 2014 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     public partial class FormMain : Form
  2.     {
  3.         private EventWrapper<KeyPressEventHandler> keyPressOn_PushButton;
  4.         private EventWrapper<MouseEventHandler> clicksOn_PushButton;
  5.  
  6.         public FormMain()
  7.         {
  8.             InitializeComponent();
  9.             OtherStuff();
  10.         }
  11.  
  12.         private void OtherStuff()
  13.         {
  14.             var clicksOn_textBoxForLog = new MouseClickEventWrapper(textBoxForLog)
  15.                 .AddHandler((sender, args) => textBoxForLog
  16.                     .AppendText("clicksOn_textBoxForLog: " + args.Button.ToString() + "\n"));
  17.  
  18.             keyPressOn_PushButton = new KeyPressEventWrapper(buttonPush)
  19.                 .AddHandler((sender, args) => textBoxForLog
  20.                     .AppendText("keyPressOn_PushButton: " + args.KeyChar.ToString() + "\n"));
  21.  
  22.             clicksOn_PushButton = new MouseClickEventWrapper(buttonPush)
  23.                 .AddHandler((sender, args) => textBoxForLog
  24.                     .AppendText("clicksOn_PushButton1: " + args.Clicks.ToString() + "\n"))
  25.                 .AddHandler((sender, args) => textBoxForLog
  26.                     .AppendText("clicksOn_PushButton2: " + args.Button.ToString() + "\n")); ;
  27.         }
  28.  
  29.         private void buttonRemove_Click(object sender, EventArgs e)
  30.         {
  31.             keyPressOn_PushButton.RemoveAllHandlers();
  32.             clicksOn_PushButton.RemoveAllHandlers();
  33.         }
  34.     }
  35.  
  36.  
  37.     public abstract class EventWrapper<THandler>
  38.     {
  39.         protected readonly Control Control;
  40.  
  41.         protected readonly List<THandler> Handlers = new List<THandler>();
  42.  
  43.         protected EventWrapper(Control control)
  44.         {
  45.             Control = control;
  46.         }
  47.  
  48.         public EventWrapper<THandler> AddHandler(THandler handler)
  49.         {
  50.             AddTheHandler(handler);
  51.             Handlers.Add(handler);
  52.             return this;
  53.         }
  54.  
  55.         public void RemoveAllHandlers()
  56.         {
  57.             foreach (var handler in Handlers)
  58.             {
  59.                 RemoveHandler(handler);
  60.             }
  61.         }
  62.  
  63.         protected abstract void AddTheHandler(THandler handler);
  64.  
  65.         protected abstract void RemoveHandler(THandler handler);
  66.     }
  67.  
  68.  
  69.  
  70.     public class KeyPressEventWrapper
  71.         : EventWrapper<KeyPressEventHandler>
  72.     {
  73.         public KeyPressEventWrapper(Control control)
  74.             : base(control)
  75.         {
  76.         }
  77.  
  78.         protected override void AddTheHandler(KeyPressEventHandler handler)
  79.         {
  80.             Control.KeyPress += handler;
  81.         }
  82.  
  83.         protected override void RemoveHandler(KeyPressEventHandler handler)
  84.         {
  85.             Control.KeyPress -= handler;
  86.         }
  87.     }
  88.  
  89.     public class MouseClickEventWrapper
  90.         : EventWrapper<MouseEventHandler>
  91.     {
  92.         public MouseClickEventWrapper(Control control)
  93.             : base(control)
  94.         {
  95.         }
  96.  
  97.         protected override void AddTheHandler(MouseEventHandler handler)
  98.         {
  99.             Control.MouseClick += handler;
  100.         }
  101.  
  102.         protected override void RemoveHandler(MouseEventHandler handler)
  103.         {
  104.             Control.MouseClick -= handler;
  105.         }
  106.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top