Merge two contextmenustrips to a single tree node

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • libishkb
    New Member
    • Mar 2010
    • 6

    Merge two contextmenustrips to a single tree node

    I have create two separate contextmenu strips on a windows form. Now i want to display both these combined on to a tree node. Is this possible?
    e.g.
    First strip: Add, Subtract
    Second strip: multiply, divide.

    When I right click on a tree node the menu should display Add,subtract,mu ltiply and divide.

    I know that
    treeView.Nodes[3].Nodes[2].ContextMenuStr ip= firststrip;
    will disply the first one only. I want to do something like this
    treeView.Nodes[3].Nodes[2].ContextMenuStr ip= firststrip+seco ndstrip
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    You will have to programmaticall y add all off the ContextMenuItem s from menu 1 and menu 2 into a new menu 3, then show that. A simple 'for' loop construct should do it.

    Comment

    • libishkb
      New Member
      • Mar 2010
      • 6

      #3
      I tried this but doesnt work. Do you find any error in this code?

      Code:
             private ContextMenuStrip mMergeContextMenuStrips(params ContextMenuStrip[] list)
              {
                  ///
                  /// Merges contextmenustrips  and return a new strip
                  ///
                  ContextMenuStrip MergeContextMenuStrip = new ContextMenuStrip();
                  for (int i = 0; i < list.Length; i++)
                  {
                      for (int j=0; j< list[i].Items.Count; j++)
                      {
                          MergeContextMenuStrip.Items.Add(list[i].Items[j]);
                      }
                  }
                  return MergeContextMenuStrip;
              }
      Last edited by tlhintoq; Mar 11 '10, 05:05 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          *** EDIT ***
          If you read my first post, ignore it :D I didn't notice you were passing an array of ContextMenuStri p objects to your method.

          Here's a better question, what doesn't work about it? It crashes, merged strip comes out empty, etc..?
          Last edited by GaryTexmo; Mar 11 '10, 05:21 PM. Reason: Read post wrong

          Comment

          • libishkb
            New Member
            • Mar 2010
            • 6

            #6
            In the code above, at the line given below

            MergeContextMen uStrip.Items.Ad d(list[i].Items[j]);
            as soon as the item[j] in the list[i] is added to MergeContextMen uStrip this item is deleted from the list[i].items. I dont want this to happen. I just want a copy of this list[i].Items[j] to be pasted in the MergeContextMen uStrip.

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              Hmm, it looks like those ToolStripMenuIt ems can only belong to one context menu at a time. You're absolutely correct, when you add from one list to the other, it just removes itself. I dug around MSDN and I don't see why this is.

              Either way, all you need to do is create a copy of each one you find and then add it to your merged list.

              I found a couple of resources on merging context menus. There's a ToolStripManage r.Merge method that does this, but it also seems to remove the items from the source and put them in the new location. So I think your best bet, if you want to maintain both lists, is to create a copy.

              (Link that might interest you: http://social.msdn.microsoft.com/For...4-3346d0749b00)

              Comment

              Working...