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 3460248
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:12:23+00:00 2026-05-18T10:12:23+00:00

My code for opening a context menu on Right-click for a treeview is as

  • 0

My code for opening a context menu on Right-click for a treeview is as follows

private void contextMenu_Opening(object sender, CancelEventArgs e)
    {
        if (Convert.ToInt16(tvwACH.SelectedNode.Tag) == 1)
        {
            contextMenu.Items.Add(New);
            contextMenu.Items.Remove(Remove);
            contextMenu.Items.Remove(Saveas);
            contextMenu.Items.Remove(Save);
            contextMenu.Items.Remove(addEntry);
        }
        if (Convert.ToInt16(tvwACH.SelectedNode.Tag) == 2)
        {
            contextMenu.Items.Add(New);
            contextMenu.Items.Remove(Remove);
            contextMenu.Items.Remove(Saveas);
            contextMenu.Items.Remove(Save);
            contextMenu.Items.Remove(addEntry);
            New.Text = "Add FileHeader";
        }
        if (Convert.ToInt16(tvwACH.SelectedNode.Tag) == 3)
        {
            contextMenu.Items.Remove(New);
            contextMenu.Items.Add(Save);
            contextMenu.Items.Add(Saveas);
            contextMenu.Items.Remove(Remove); //Added later
            contextMenu.Items.Remove(addEntry);
        }
        if (tvwACH.SelectedNode.Parent != null)
        {
            string str = tvwACH.SelectedNode.Parent.ToString().Substring(10);
            if (str == "BatchHeader")
            {
                contextMenu.Items.Remove(New);
                contextMenu.Items.Remove(Save);
                contextMenu.Items.Remove(Remove);
                contextMenu.Items.Remove(Saveas);
                contextMenu.Items.Add(addEntry);
            }
        }

and also mouse down for treeview as follows

 private void tvwACH_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            contextMenu.Show(tvwACH, e.Location);
        }
        location = e.Location;
    }

But this getting opened every where on tree-view control but i only need it to be opened when i click on Nodes of my tree-view.

Any help please

  • 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-05-18T10:12:24+00:00Added an answer on May 18, 2026 at 10:12 am

    If you only want the context menu to be displayed when the user right-clicks on a node, you need to include some logic in your MouseDown event hander to verify that the click event occurred over a node.

    You can determine the node that is located at a particular point using the HitTest method provided by the TreeView. For example, you could modify your current MouseDown event handler to include the following code:

    private void tvwACH_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            if (tvwACH.HitTest(e.Location).Node != null)
            {
                contextMenu.Show(tvwACH, e.Location);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.