Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8048623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:30:03+00:00 2026-06-05T06:30:03+00:00

I have a TreeView where all nodes are assigned the same ContextMenuStrip. When I

  • 0

I have a TreeView where all nodes are assigned the same ContextMenuStrip.

When I right-click a TreeNode other than the one already selected and then select an item in the ContextMenuStrip, treeView1.SelectedNode is for the node that was selected before right clicking and not for the item that was right clicked.

How do I change this behavior so that it will correctly select the right clicked node before showing the ContextMenuStrip?

private void newGroupToolStripMenuItem_Click(object sender, EventArgs e)
{
    TextFieldDialog t = new TextFieldDialog("New Order Group", "Enter the name of the new group");
    t.ShowDialog();
    if (t.DialogResult == DialogResult.OK)
    {
        int lastid = db.NewOrderGroup(t.Value, (int)treeView1.SelectedNode.Tag);
        TreeNode node = new TreeNode(t.Value);
        node.Tag = lastid;
        treeView1.SelectedNode.Nodes.Add(node);
    }
}

In the above code (int)treeView1.SelectedNode.Tag returns the tag of the Node that was selected before I right clicked, and not the Node that was right clicked.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:30:05+00:00Added an answer on June 5, 2026 at 6:30 am

    It is a quirk of TreeView, only the left mouse button selects a node. Reset the ContextMenuStrip property and note the behavior, when you right-click the highlight does jump to the clicked node but it jumps right back after you release the button.

    Fix it by implementing an event handler for the NodeMouseClick event. Select the node and show the context menu, like this:

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
        if (e.Button == MouseButtons.Right) {
            treeView1.SelectedNode = e.Node;
            contextMenuStrip1.Show(treeView1, e.Location);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My TreeView nodes have a checkbox. If the User checks one checkbox, all other
I Have a treeView Already populated (in vb.net) and would Like to move all
All my TreeView nodes have a unique ID for their Node Depth. I want
I have a treeview in wpf and I load it ok is all done
I have a treeView with many nodes. I want that some nodes change their
I'm experimenting with a treeview in a little C#/Winforms application. I have programatically assigned
I am creating a TreeView at runtime. It has several nodes(TreeViewItem), each one having
I have a TreeView which will be populated with some Nodes. The problem is,
If I have a TreeView (myTreeview),how can I obtain a list of all parent
I have a TreeView with some nodes inside of it. Whenever the user clicks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.