Solved

Middle Mouse clicking on a node in a TreeView

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

790 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