Detecting Mouse Button Events in C# | CodeGuru

Detecting Mouse Button Events in C#

MouseButtonTest 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 […]

Written By
CodeGuru Staff
CodeGuru Staff
May 23, 2003
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

MouseButtonTest

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.