title: MouseHandler Events and Delegates c#
body: <p>I have been looking at events and delegates over the last few days and I decided to attempt to make a MouseHandler class using both of these. </p>
  
  <p>Basically I have this problem where the mouse clicks are not registering and the positions for the x and y axis are coming back as 0, by not registering I simply mean displaying in the output console through console.WriteLine(). I have been researching for a while now but I have had no luck so I was wondering if any of you guys could point me in the right direction it would be greatly appreciated.</p>
  
  <p>Thank you in advance. </p>
  
  <p><strong>MouseHandler.cs</strong></p>
  
  <pre><code>class MouseHandler
      {
  
          public delegate void MouseEventHandler(object source, MouseEventHandlerArgs 
  
          public event MouseEventHandler MouseLeftClick;
          public event MouseEventHandler MouseRightClick;
          public event MouseEventHandler MouseMoved;
  
          public MouseState mouseState = Mouse.GetState();
  
          protected virtual void OnMouseLeftClick(MouseState m)
          {
              mouseState = m;
              if(MouseLeftClick != null)
              {
                 MouseLeftClick(this, new MouseEventHandlerArgs() { MouseButtonPress = m });
  
              }
          }
  
          protected virtual void OnMouseMoved(MouseState m)
          {
              if (MouseMoved != null)
              {
  
                  MouseMoved(this, new MouseEventHandlerArgs(){MouseButtonPress = m  });
                 Console.WriteLine(mouseState.Position.X);
              }
          }
  
          public void Update(GameTime gt)
          {
              OnMouseLeftClick(mouseState);
              OnMouseMoved(mouseState);
          }
      }
  </code></pre>
  
  <p><strong>MouseEventHandlerArgs</strong></p>
  
  <pre><code>class MouseEventHandlerArgs : EventArgs
  {
      public MouseState MouseButtonPress { get; set; }
  
  
  }
  </code></pre>
  
  <p><strong>MouseTest</strong></p>
  
  <pre><code>class MouseTest
      {
          public void OnMouseLeftClick(object source, MouseEventHandlerArgs e)
          {
              Console.WriteLine("Mouse Left click" + e.MouseButtonPress.LeftButton);
          }        
  
          public void OnMouseMoved(object source, MouseEventHandlerArgs e)
          {
              Console.WriteLine("you moved mouse to x " + e.MouseButtonPress.Position.X + " Y pos " + e.MouseButtonPress.Position.Y );
          }
      }
  </code></pre>
  
  <p><strong>Game1</strong>
  Within the update method: </p>
  
  <pre><code>mouseHandler.MouseLeftClick += mouseTest.OnMouseLeftClick;
  mouseHandler.MouseMoved += mouseTest.OnMouseMoved;
  //mouseHandler.Update(gameTime);
  </code></pre>