Solved

Middle Mouse clicking on a node in a TreeView

Posted on 2009-07-01
5
604 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 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 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