Why am I getting out of range exception with these code?

G

Guest

Hi: Below is the error I got from the 2 lines of code below. I don't
understand why and how to correct it. The actionMenu.DropDownItems has 0
item in its collection at the time of the code. Thanks.

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred
in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and
less than the size of the collection.


ToolStripItemCollection contextsMenuItems =
contextsMenuStrip.Items;
.AddRange(contextsMenuItems);
 
J

Jon Skeet [C# MVP]

Pucca said:
Hi: Below is the error I got from the 2 lines of code below. I don't
understand why and how to correct it. The actionMenu.DropDownItems has 0
item in its collection at the time of the code. Thanks.

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred
in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and
less than the size of the collection.


ToolStripItemCollection contextsMenuItems =
contextsMenuStrip.Items;
.AddRange(contextsMenuItems);

..AddRange(contextsMenuItems) isn't a valid statement, to start with.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
G

Guest

I want to Change the dropdown menuItems from the menustrip's 1st item
(Action) to be the same as the contextmenustrip that would pop-up for the
node selected in a Treeview control. Here is the complete method. Thank you
very much.

private void ppTree_NodeMouseClick(object sender,
TreeNodeMouseClickEventArgs e)
{
int index = 0;
string nodeTxt = e.Node.Text;
ToolStripMenuItem actionMenu =
(ToolStripMenuItem)mainMenu.Items[0];
actionMenu.DropDownItems.Clear();

if (nodeTxt != null)
{
switch (nodeTxt)
{
case "Unity Administration":
ToolStripItemCollection menuItems =
rootMenuStrip.Items;
actionMenu.DropDownItems.
while (menuItems.Count >= 0)
{
actionMenu.DropDownItems.Add(menuItems[index]);
index++;
}
if (e.Button.ToString() == "Right")
rootMenuStrip.Show(ppTree, e.Location);
break;
case "Contexts":
ToolStripItemCollection contextsMenuItems =
contextsMenuStrip.Items;

//actionMenu.DropDownItems.AddRange(contextsMenuItems);
while (contextsMenuItems.Count > 0)
{

actionMenu.DropDownItems.Add(contextsMenuItems[index]);
index++;
}
if (e.Button.ToString() == "Right")
contextsMenuStrip.Show(ppTree, e.Location);
break;
case "Computers":
ToolStripItemCollection computersMenuItems =
contextsMenuStrip.Items;
actionMenu.DropDownItems.AddRange(computersMenuItems);
if (e.Button.ToString() == "Right")
RefreshHelpMenuStrip.Show(ppTree, e.Location);
break;
case "Groups":
ToolStripItemCollection groupsMenuItems =
contextsMenuStrip.Items;
actionMenu.DropDownItems.AddRange(groupsMenuItems);
if (e.Button.ToString() == "Right")
RefreshHelpMenuStrip.Show(ppTree, e.Location);
break;
case "Users":
ToolStripItemCollection usersMenuItems =
contextsMenuStrip.Items;
actionMenu.DropDownItems.AddRange(usersMenuItems);
if (e.Button.ToString() == "Right")
RefreshHelpMenuStrip.Show(ppTree, e.Location);
break;
case "Reports":
ToolStripItemCollection reportsMenuItems =
contextsMenuStrip.Items;
actionMenu.DropDownItems.AddRange(reportsMenuItems);

if (e.Button.ToString() == "Right")
reportsMenuStrip.Show(ppTree, e.Location);
break;
default://Maybe it's a context node
if (ppTree.SelectedNode.Parent != null)
{
if (ppTree.SelectedNode.Parent.Name == "Contexts")
contextMenuStrip.Show(ppTree, e.Location);
}
//test.MyTestMethod("COM INTEROP TEST");
break;
}//end switch
}
}
 
J

Jon Skeet [C# MVP]

Pucca said:
I want to Change the dropdown menuItems from the menustrip's 1st item
(Action) to be the same as the contextmenustrip that would pop-up for the
node selected in a Treeview control. Here is the complete method. Thank you
very much.

1) That's not a complete *program*
2) It's almost certainly far longer than it needs to be to demonstrate
the program.

See http://www.pobox.com/~skeet/csharp/complete.html
 
G

Guest

OK, got it. Something like this for my future posting of codes. Thanks.
private void ppTree_NodeMouseClick(object sender,
TreeNodeMouseClickEventArgs e)
{
int index = 0;
string nodeTxt = e.Node.Text;
ToolStripMenuItem actionMenu = (ToolStripMenuItem)mainMenu.Items[0];
actionMenu.DropDownItems.Clear();
if (nodeTxt != null)
{
switch (nodeTxt)
{


case "Computers":
ToolStripItemCollection computersMenuItems =
contextsMenuStrip.Items;
actionMenu.DropDownItems.AddRange(computersMenuItems);
if (e.Button.ToString() == "Right")
RefreshHelpMenuStrip.Show(ppTree, e.Location);
break;
}
}
 
L

Lebesgue

Did you happen to read Jon's article on what is a complete program?
This is not yet a complete program, maybe next time you'll make it. Try to
really read the article before posting again.

Hint: There's something like "They should compile" in the rules.

Pucca said:
OK, got it. Something like this for my future posting of codes. Thanks.
private void ppTree_NodeMouseClick(object sender,
TreeNodeMouseClickEventArgs e)
{
int index = 0;
string nodeTxt = e.Node.Text;
ToolStripMenuItem actionMenu = (ToolStripMenuItem)mainMenu.Items[0];
actionMenu.DropDownItems.Clear();
if (nodeTxt != null)
{
switch (nodeTxt)
{


case "Computers":
ToolStripItemCollection computersMenuItems =
contextsMenuStrip.Items;
actionMenu.DropDownItems.AddRange(computersMenuItems);
if (e.Button.ToString() == "Right")
RefreshHelpMenuStrip.Show(ppTree, e.Location);
break;
}
}
--
Thanks.


Jon Skeet said:
1) That's not a complete *program*
2) It's almost certainly far longer than it needs to be to demonstrate
the program.

See http://www.pobox.com/~skeet/csharp/complete.html
 
C

Cor Ligthert [MVP]

Pucca,

To get an System.ArgumentOutOfRangeException with treeviews and things like
that is an often showed problem in these newsgroups.

It comes mostly because when you add a node you start a recursive loop if
there is an event that catches that. Have a look if you have somewhere an
event that start if there is something added to your treeview.

For that I don't need to see code, it can be on some strange places you
know.

I hope this helps,

Cor
 
G

Guest

Thanks. I think you're right. When I have some time I'll look into it some
more Thank you.
 

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