Solved

Middle Mouse clicking on a node in a TreeView

Posted on 2009-07-01
5
600 Views
Last Modified: 2013-12-17
I have a TreeView in a Windows Forms application in VS2005.
I am trying to display a message if the MIDDLE mouse button is clicked.

IT works OK for left and for right.
Why will it not work for the middle mouse button?


private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)

        {

            switch (e.Button)

            {

                case MouseButtons.Left:

                    MessageBox.Show("Left clicked");

                    break;

                case MouseButtons.Middle:

                    MessageBox.Show("Middle clicked"); //Code is not reached

                    break;

                case MouseButtons.None:

                    break;

                case MouseButtons.Right:

                    MessageBox.Show("Right clicked");

                    break;

                case MouseButtons.XButton1:

                    break;

                case MouseButtons.XButton2:

                    break;

                default:

                    break;

            }

        }

Open in new window

0
Comment
Question by:Dabas
5 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
I'm not sure why it doesn't work for MouseClick (perhaps it's a bug), but it does work if you use MouseDown or MouseUp instead.
0
 
LVL 27

Author Comment

by:Dabas
Comment Utility
I need it to work on the node, thanks
0
 
LVL 15

Accepted Solution

by:
jinal earned 400 total points
Comment Utility

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
MessageBox.Show(e.Button.ToString() + e.Node.Text);
}
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Middle)
{
TreeNodeMouseClickEventArgs arg1 = new TreeNodeMouseClickEventArgs(treeView1.SelectedNode,MouseButtons.Middle,1,MousePosition.X,MousePosition.Y);
treeView1_NodeMouseClick(sender,arg1);
}
}
private void treeView1_NodeMouseHover(object sender, TreeNodeMouseHoverEventArgs e)
{
treeView1.SelectedNode = e.Node;
}
0
 
LVL 9

Assisted Solution

by:Rahul Goel
Rahul Goel earned 100 total points
Comment Utility
Here I created a new custom control.you can use that
    class treeView : TreeView

    {

        public event EventHandler OnMiddleMouseClick; 
 

        protected override void WndProc(ref Message m)

        {

            if (m.Msg == 0x207)

            {

                if (OnMiddleMouseClick != null)

                    OnMiddleMouseClick(this, new EventArgs());

            }

            base.WndProc(ref m);

        }

    }

Open in new window

0
 
LVL 27

Author Comment

by:Dabas
Comment Utility
jinal:

Your solution works, thanks!

Rahu_ketu:

Your solution also works, but I prefer jinal's as I can have all of the buttons handled in the one place. Also, the code lets me know which node has been clicked.

Dabas
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now