473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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);
--
Thanks.
Nov 3 '06 #1
7 5589
Pucca <Pu***@discussions.microsoft.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 3 '06 #2
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(contextsMenuItem s);
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(computersMenuIte ms);
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
}
}
--
Thanks.
"Jon Skeet [C# MVP]" wrote:
Pucca <Pu***@discussions.microsoft.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 3 '06 #3
Pucca <Pu***@discussions.microsoft.comwrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 3 '06 #4
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(computersMenuIte ms);
if (e.Button.ToString() == "Right")
RefreshHelpMenuStrip.Show(ppTree, e.Location);
break;
}
}
--
Thanks.
"Jon Skeet [C# MVP]" wrote:
Pucca <Pu***@discussions.microsoft.comwrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 3 '06 #5
Did you happen to read Jon's article on what is a complete program?
See http://www.pobox.com/~skeet/csharp/complete.html
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" <Pu***@discussions.microsoft.comwrote in message
news:9E**********************************@microsof t.com...
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(computersMenuIte ms);
if (e.Button.ToString() == "Right")
RefreshHelpMenuStrip.Show(ppTree, e.Location);
break;
}
}
--
Thanks.
"Jon Skeet [C# MVP]" wrote:
>Pucca <Pu***@discussions.microsoft.comwrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 3 '06 #6
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

"Pucca" <Pu***@discussions.microsoft.comschreef in bericht
news:A8**********************************@microsof t.com...
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);
--
Thanks.

Nov 4 '06 #7
Thanks. I think you're right. When I have some time I'll look into it some
more Thank you.
--
Thanks.
"Cor Ligthert [MVP]" wrote:
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

"Pucca" <Pu***@discussions.microsoft.comschreef in bericht
news:A8**********************************@microsof t.com...
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);
--
Thanks.


Nov 6 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andrew MacIntyre | last post by:
I'm seeing a bizarre situation where IndexErrors are being thrown with "tuple index out of range" error strings. The scenario is something like: l = for a, b in l: ...
15
by: Xah Lee | last post by:
Here's the belated Java solution. import java.util.List; import java.util.ArrayList; import java.lang.Math; class math { public static List range(double n) { return range(1,n,1); }
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
4
by: kscdavefl | last post by:
I ahve a datagrid on a web form. I need to change the value in column 3 as follows. If the value in column 3 reads 0, I want to change it to read YES. How can I accomplish this task. ...
4
by: IMS.Rushikesh | last post by:
Hi All, I am trying to execute below code but it gives me an COMException ///// Code Start //// public string GetName(Excel.Range range) { try { if (range.Name != null)
3
by: George | last post by:
Sub ExcelToListBox() Dim xRange As Object Dim ary Dim xValue As String xRange = oXL.Range("A1:A9") 'has letters A-H ary = xRange.value xValue = ary(3, 1) 'xValue = C...
3
by: | last post by:
I wrote a class in VB.NET to export the contents of a datagrid to Excel. It works perfectly on my machine, but it fails on my customers' PCs that have identical versions of Win XP (SP1) and Excel...
2
by: srusskinyon | last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for...
1
by: =?Utf-8?B?SkI=?= | last post by:
Hello As I debug the C# code with a break point and by pressing F11 I eventually get a message stating: ContextSwitchDeadlock was detected Message: The CLR has been unable to transition from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.