Search code examples
vb.netcontextmenustrip

Transfer items between contextmenustrips vb.net


I want to transfer items of contextmenustrip to other using items.addrange method. I can transfer items one by one using a for loop:

For each Item in ContextMenuStrip1.items
ContextMenuStrip2.Items.Add(item)
Next

But how I can transfer items using items.addrange method?


Solution

  • You need to copy the elements from the original ContextMenuStrip in an array (CopyTo seems enough for this), Then it is simple to add the array to the second ContextMenuStrip

    Dim tsi(ContextMenuStrip1.Items.Count - 1) As ToolStripItem
    ContextMenuStrip1.Items.CopyTo(tsi, 0)
    ContextMenuStrip2.Items.AddRange(tsi)