C# WinForms: Using both a Context Menu and the AfterSelect event

Posted on 2012-09-13
Last Modified: 2012-09-13
I want to open a dislog when the user clicks a certain node type in a TreeView.  And I want a right click to open a context menu.  I have each working, but they collide with each other.

How can I solve this problem?

I do not want a left click to open the Context Menu.  This is what create the collission.

Question by:newbieweb
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    So you want two different behaviors for a RIGHT click on a TreeView node based on the node type?

    Author Comment

    No, I want to open the dialog AfterSelect on a left click, based on node type (not all node types have a corresponding dialog). I want to display the Context Menu on a right click.
    LVL 85

    Expert Comment

    by:Mike Tomlinson

    "I do not want a left click to open the Context Menu.  This is what create the collission."

    I don't see how these collide.  Left click does not normally open a context menu.  How are you assigning/displaying the context menu?  What is in AfterSelect()?
    LVL 85

    Accepted Solution

    I see...when you right click a node it temporarily selects that node causing the AfterSelect() event to fire.

    One workaround is to use MouseUp() instead of AfterSelect() like this:
            private void treeView1_MouseUp(object sender, MouseEventArgs e)
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                    TreeNode tn = treeView1.GetNodeAt(treeView1.PointToClient(Cursor.Position));
                    if (tn != null && tn == treeView1.SelectedNode)
                        Console.WriteLine("LeftMouseUp: " + tn.Text);

    Open in new window

    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    Another option is to manually display the ContextMenu in response to a Right Click (instead of assigning it and letting it display automatically).

    Author Closing Comment

    LVL 13

    Expert Comment

    by:Naman Goel
    I tried following code and it is not showing any collision

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    namespace Test
        public partial class Form1 : Form
            private ContextMenuStrip contextMenuStrip1;
            private IContainer components;
            private ToolStripMenuItem toolStripMenuItem2;
            private ToolStripMenuItem toolStripMenuItem3;
            private ToolStripMenuItem toolStripMenuItem4;
            private TreeView treeView1;
            public Form1()
            private void InitializeComponent()
                this.components = new System.ComponentModel.Container();
                System.Windows.Forms.TreeNode treeNode1 = new System.Windows.Forms.TreeNode("Node1");
                System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Node3");
                System.Windows.Forms.TreeNode treeNode3 = new System.Windows.Forms.TreeNode("Node2", new System.Windows.Forms.TreeNode[] {
                System.Windows.Forms.TreeNode treeNode4 = new System.Windows.Forms.TreeNode("Node0", new System.Windows.Forms.TreeNode[] {
                System.Windows.Forms.TreeNode treeNode5 = new System.Windows.Forms.TreeNode("Node6");
                System.Windows.Forms.TreeNode treeNode6 = new System.Windows.Forms.TreeNode("Node5", new System.Windows.Forms.TreeNode[] {
                System.Windows.Forms.TreeNode treeNode7 = new System.Windows.Forms.TreeNode("Node4", new System.Windows.Forms.TreeNode[] {
                this.treeView1 = new System.Windows.Forms.TreeView();
                this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
                this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
                this.toolStripMenuItem3 = new System.Windows.Forms.ToolStripMenuItem();
                this.toolStripMenuItem4 = new System.Windows.Forms.ToolStripMenuItem();
                // treeView1
                this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.treeView1.Location = new System.Drawing.Point(0, 0);
                this.treeView1.Name = "treeView1";
                treeNode1.Name = "Node1";
                treeNode1.Text = "Node1";
                treeNode2.Name = "Node3";
                treeNode2.Text = "Node3";
                treeNode3.Name = "Node2";
                treeNode3.Text = "Node2";
                treeNode4.Name = "Node0";
                treeNode4.Text = "Node0";
                treeNode5.Name = "Node6";
                treeNode5.Text = "Node6";
                treeNode6.Name = "Node5";
                treeNode6.Text = "Node5";
                treeNode7.Name = "Node4";
                treeNode7.Text = "Node4";
                this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
                this.treeView1.Size = new System.Drawing.Size(284, 262);
                this.treeView1.TabIndex = 0;
                this.treeView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseDown);
                // contextMenuStrip1
                this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.contextMenuStrip1.Name = "contextMenuStrip1";
                this.contextMenuStrip1.Size = new System.Drawing.Size(81, 70);
                // toolStripMenuItem2
                this.toolStripMenuItem2.Name = "toolStripMenuItem2";
                this.toolStripMenuItem2.Size = new System.Drawing.Size(80, 22);
                this.toolStripMenuItem2.Text = "1";
                // toolStripMenuItem3
                this.toolStripMenuItem3.Name = "toolStripMenuItem3";
                this.toolStripMenuItem3.Size = new System.Drawing.Size(80, 22);
                this.toolStripMenuItem3.Text = "2";
                // toolStripMenuItem4
                this.toolStripMenuItem4.Name = "toolStripMenuItem4";
                this.toolStripMenuItem4.Size = new System.Drawing.Size(80, 22);
                this.toolStripMenuItem4.Text = "3";
                // Form1
                this.ClientSize = new System.Drawing.Size(284, 262);
                this.Name = "Form1";
            private void treeView1_MouseDown(object sender, MouseEventArgs e)
                if (int.Parse(treeView1.SelectedNode.Text.Substring(treeView1.SelectedNode.Text.Length - 1)) % 2 == 0)
                    if (e.Button == System.Windows.Forms.MouseButtons.Right)
                        contextMenuStrip1.Show(treeView1, e.Location);
                        MessageBox.Show(this,"Test", "Test");

    Open in new window


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    729 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

    20 Experts available now in Live!

    Get 1:1 Help Now