Detecting Mouse Button Events in C#

MouseButtonTest



Click here for larger image

This is a simple program I wrote when I needed to detect the right mouse button click in a listbox and couldn't find it in the VS.NET IDE. In the documentation I came across a class called "MouseEventArgs".

MouseEventArgs is passed to a MouseEventHandler defined for the listbox. In the InitializeComponent() method for the form is the definition and initialization of the components of the form. I added The following to this section:

this.m_listbox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ButtonDown);

This sets the method ButtonDown to receive all mouse button events. There I determine what button was pressed, what row was clicked on, and display the results. The only events I see useful are left, right, and middle. Microsoft added two more events, XButton1 and XButton2, for it's own devices. The documentation even has a 'none' event, which I cannot fathom. That's about it.

//---------------------------------------------------------------------
private void OKButtonClick(object sender, System.EventArgs e)
{
   Application.Exit();
}
//---------------------------------------------------------------------
private void ButtonDown(object sender, MouseEventArgs mea)
{
   int nDx = 0; //This will be the 0 based index of the row clicked on
    if (mea.Button == MouseButtons.Right) { //Check for right button click
        MessageBeep(16);
        m_mouse_event.Text = "Right Button Clicked\r\n";
    }
    else if (mea.Button == MouseButtons.Left) { //Check for Left button click
        MessageBeep(256);
        m_mouse_event.Text = "Left Button Clicked\r\n";
    }
 
    nDx = mea.Y / m_listbox.ItemHeight; //Get Index of item selected
    m_mouse_event.Text = m_mouse_event.Text +
            "Item height = " + m_listbox.ItemHeight.ToString()+ "\r\n" +
            "X position = " + mea.X.ToString() + "\r\n" +
            "Y position = " + mea.Y.ToString() + "\n" +"\r\n"+
            "Index = " + nDx.ToString();
 
    if ( m_listbox.Items.Count <= nDx ) m_mouse_event.Text = m_mouse_event.Text + "\r\n" +
            "Row Clicked Beyond Table size";
    else m_listbox.SetSelected(nDx,true);         //highlight row selected
}
//-------------------------------------------------------------------

 


Downloads

Download demo project - 79 Kb
Download source - 8 Kb


Comments

  • Comment protocol needs overhaul..

    Posted by Legacy on 05/29/2003 12:00am

    Originally posted by: Chris

    Obviously, the ability for just anyone to post a comment is being abused. It's a shame to see someone who takes the time and effort to post an article be insulted by juvenile behaviour.

    Maybe only registered members who are signed in should be able to post a comment? I'm sure this would cut down on the abuse.

    ( Of course, one has to wonder: Are the comments posted by 'Bill Gates' from the *real* Bill Gates of Microsoft fame? I wouldn't be surprised! )

    Reply
  • Thanks for spending your time

    Posted by Legacy on 05/27/2003 12:00am

    Originally posted by: Bryan Hinton

    Thanks for spending your time writing the article - I found it interesting and just wanted to post something positive compared to whoever this other poster was.  
    

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds