Solved

Middle Mouse clicking on a node in a TreeView

Posted on 2009-07-01
5
610 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24760488
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
ID: 24760623
I need it to work on the node, thanks
0
 
LVL 15

Accepted Solution

by:
jinal earned 400 total points
ID: 24761397

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 ITIL
Rahul Goel ITIL earned 100 total points
ID: 24761520
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
ID: 24761692
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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