CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Context Menu

  1. #1
    Join Date
    Dec 2003
    Posts
    87

    Context Menu

    I am trying to display a different context menu for different tree nodes. I can distinguish what node has been selected but the context menu is displaying the context menu for the last node selected. After searching the forums I saw that you have to place:

    treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);

    in the mousedown event. I still have the same result. I stepped through the mouse down code to ensure it is working and it is.

    *********************
    On mouse up I verify the right mouse button:
    *********************

    if(e.Button == MouseButtons.Right)
    {

    *********************
    I then see what type of node by checking the .Tag property:
    *********************

    TreeNodeTypeStruct newTreeNodeTypeStruct = new TreeNodeTypeStruct();

    newTreeNodeTypeStruct = (TreeNodeTypeStruct) treeView1.SelectedNode.Tag;

    if(newTreeNodeTypeStruct.getTreeNodeTypeStruct() == TreeNodeType.System)
    {

    *********************
    I create the context menu:
    *********************

    MenuItem miAddZone = new MenuItem("Add Zone",
    new EventHandler(OnAddZoneNode));

    cm1.MenuItems.Add(miAddZone);

    //EventHandler ehAddZone = new EventHandler (OnAddZoneNode);
    //cm1.MenuItems.Add("Add Zone", ehAddZone);
    ContextMenu = cm1;


    *********************
    The event method:
    *********************


    void OnAddZoneNode(object obj, EventArgs ea)
    {
    TreeNode newNode = treeView1.SelectedNode.Nodes.Add("Zone");
    TreeNodeTypeStruct myTreeNodeTypeStruct = new TreeNodeTypeStruct(TreeNodeType.Zone);
    newNode.Tag = myTreeNodeTypeStruct;
    }



    *********************
    During debug the functions are stepping through but the wrong menu is being displayed.

    Any advice??

  2. #2
    Join Date
    Jan 2001
    Location
    Germany
    Posts
    222
    Greetings,

    I was able to work around that problem by showing the context menu manually, thus not setting the tree view's ContextMenu property.
    Code:
    TreeNode node = treeView1.GetNodeAt(e.X, e.Y);
    if (node != null)
    {
        MenuItem item = new MenuItem(node.FullPath);
        cm1.MenuItems.Clear();
        cm1.MenuItems.Add(item);
        cm1.Show(treeView1, new Point(e.X, e.Y));
    }
    Teamwork Software - Stuff That Does Something

  3. #3
    Join Date
    Dec 2003
    Posts
    87

    Talking

    Captain Nuss,

    Thank you so much!!!

    I was pulling my hair out with this. It works GREAT!!

    Matt

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