CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2008
    Posts
    154

    [SOLVED] Right Click in TreeView not responding

    C# WinForms.. trying to get a contextMenu to show in TreeView when I right click - but nothing is happening

    Code:
            private void mainView_MouseClick(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (mainView.SelectedNode == null)
                    {
                        Point ClickPoint = new Point(e.X, e.Y);
                        Point ScreenPoint = mainView.PointToScreen(ClickPoint);
                        Point FormPoint = this.PointToClient(ScreenPoint);
                        cntxtMenuTreeNPCCreate.Show(this, FormPoint);
                    }
                }
            }
    Last edited by bixel; January 3rd, 2010 at 07:15 PM.

  2. #2
    Join Date
    Jun 2008
    Posts
    154

    Re: Right Click in TreeView not responding

    Solved!

    I initially created the Event in the TreeView's MouseClick Event, however I doubleclicked the TreevView's MouseDown Event to create the method and copied the same code over. Apparently it works like a charm in MouseDown - but will not register in MouseClick

    Code:
            private void mainView_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (mainView.SelectedNode == null)
                    {
                        Point ClickPoint = new Point(e.X, e.Y);
                        Point ScreenPoint = mainView.PointToScreen(ClickPoint);
                        Point FormPoint = this.PointToClient(ScreenPoint);
                        cntxtMenuTreeNPCCreate.Show(this, FormPoint);
                    }
                }
            }

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Right Click in TreeView not responding

    The problem with using the mouse down or the mouse click events is that it won't display the context menu when the user uses the keyboard SHIFT+F10 command.

    So why not hook up the context menu using the TreeView.ContextMenuStrip property?

    If you need a dynamic context menu (where the menu items change depending on the selected tree item), then override the AfterSelect event handler and change the context menu strip items based on the selected node.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured