• Advertisement
Sign in to follow this  

Mouse input in C#

This topic is 3861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I feel pretty retarded, but I can't get mouse input working in C#. Here's my really simple test app:
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace mousetest
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            MouseEventListener listener = new MouseEventListener();
            mouseHandler += new MouseEventHandler(listener.HandleMouseClick);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
        
        public static event MouseEventHandler mouseHandler;

        public class MouseEventListener
        {
            public void HandleMouseClick(object o, MouseEventArgs args)
            {
                int i = 0; //breakpoint here never gets triggered
            }
        };
    }
}

I suspect I'm missing something stupid. Little help? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
You've just defined your own event and attached to it, but that event will not get triggered by anyone else. I belive you really want to attach to the Form's mouse click instead like so:


using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Test1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

MouseEventListener listener = new MouseEventListener();
Form1 form = new Form1();
form.MouseClick += new MouseEventHandler(listener.HandleMouseClick);
Application.Run(form);
}

public static event MouseEventHandler mouseHandler;

public class MouseEventListener
{
public void HandleMouseClick(object o, MouseEventArgs args)
{
int i = 0; //breakpoint here never gets triggered
}
};

}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement