Possible bug in ToolStrip.Items.AddRange ?

J

Jon Davis

I'm taking the items from a menustrip of a control and moving them to the
parent form's menustrip:

private void TakeMenus(AdminControl ac)
{
foreach (Control c in ac.Controls)
{
if (c is MenuStrip)
{
MenuStrip ms = (MenuStrip)c;
MainMenu.Items.AddRange(ms.Items);
ac.Controls.Remove(c);
}
}
}

Works perfectly. Then I have an almost identical routine to do the same for
ToolStrip:

private void TakeToolbars(AdminControl ac)
{
foreach (Control c in ac.Controls)
{
if (c is ToolStrip)
{
ToolStrip ts = (ToolStrip)c;
ac.Controls.Remove(c);
MainToolStrip.Items.AddRange(ts.Items);
}
}
}

This gives me an ArgumentOutOfRange exception!! I worked around it with the
following:

private void TakeToolbars(AdminControl ac)
{
foreach (Control c in ac.Controls)
{
if (c is ToolStrip)
{
ToolStrip ts = (ToolStrip)c;
for (int i = 0; i < ts.Items.Count; i++)
{
ToolStripItem tsi = ts.Items;
MainToolStrip.Items.Add(tsi);
i--;
}
ac.Controls.Remove(c);
}
}
}

That was weird....

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top