473,803 Members | 4,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Drop DownItems has 0
item in its collection at the time of the code. Thanks.

An unhandled exception of type 'System.Argumen tOutOfRangeExce ption' occurred
in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and
less than the size of the collection.
ToolStripItemCo llection contextsMenuIte ms =
contextsMenuStr ip.Items;
.AddRange(conte xtsMenuItems);
--
Thanks.
Nov 3 '06 #1
7 5622
Pucca <Pu***@discussi ons.microsoft.c omwrote:
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.Drop DownItems has 0
item in its collection at the time of the code. Thanks.

An unhandled exception of type 'System.Argumen tOutOfRangeExce ption' occurred
in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and
less than the size of the collection.
ToolStripItemCo llection contextsMenuIte ms =
contextsMenuStr ip.Items;
.AddRange(conte xtsMenuItems);
..AddRange(cont extsMenuItems) 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.co m>
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 contextmenustri p that would pop-up for the
node selected in a Treeview control. Here is the complete method. Thank you
very much.

private void ppTree_NodeMous eClick(object sender,
TreeNodeMouseCl ickEventArgs e)
{
int index = 0;
string nodeTxt = e.Node.Text;
ToolStripMenuIt em actionMenu =
(ToolStripMenuI tem)mainMenu.It ems[0];
actionMenu.Drop DownItems.Clear ();

if (nodeTxt != null)
{
switch (nodeTxt)
{
case "Unity Administration" :
ToolStripItemCo llection menuItems =
rootMenuStrip.I tems;
actionMenu.Drop DownItems.
while (menuItems.Coun t >= 0)
{
actionMenu.Drop DownItems.Add(m enuItems[index]);
index++;
}
if (e.Button.ToStr ing() == "Right")
rootMenuStrip.S how(ppTree, e.Location);
break;
case "Contexts":
ToolStripItemCo llection contextsMenuIte ms =
contextsMenuStr ip.Items;

//actionMenu.Drop DownItems.AddRa nge(contextsMen uItems);
while (contextsMenuIt ems.Count 0)
{

actionMenu.Drop DownItems.Add(c ontextsMenuItem s[index]);
index++;
}
if (e.Button.ToStr ing() == "Right")
contextsMenuStr ip.Show(ppTree, e.Location);
break;
case "Computers" :
ToolStripItemCo llection computersMenuIt ems =
contextsMenuStr ip.Items;
actionMenu.Drop DownItems.AddRa nge(computersMe nuItems);
if (e.Button.ToStr ing() == "Right")
RefreshHelpMenu Strip.Show(ppTr ee, e.Location);
break;
case "Groups":
ToolStripItemCo llection groupsMenuItems =
contextsMenuStr ip.Items;
actionMenu.Drop DownItems.AddRa nge(groupsMenuI tems);
if (e.Button.ToStr ing() == "Right")
RefreshHelpMenu Strip.Show(ppTr ee, e.Location);
break;
case "Users":
ToolStripItemCo llection usersMenuItems =
contextsMenuStr ip.Items;
actionMenu.Drop DownItems.AddRa nge(usersMenuIt ems);
if (e.Button.ToStr ing() == "Right")
RefreshHelpMenu Strip.Show(ppTr ee, e.Location);
break;
case "Reports":
ToolStripItemCo llection reportsMenuItem s =
contextsMenuStr ip.Items;
actionMenu.Drop DownItems.AddRa nge(reportsMenu Items);

if (e.Button.ToStr ing() == "Right")
reportsMenuStri p.Show(ppTree, e.Location);
break;
default://Maybe it's a context node
if (ppTree.Selecte dNode.Parent != null)
{
if (ppTree.Selecte dNode.Parent.Na me == "Contexts")
contextMenuStri p.Show(ppTree, e.Location);
}
//test.MyTestMeth od("COM INTEROP TEST");
break;
}//end switch
}
}
--
Thanks.
"Jon Skeet [C# MVP]" wrote:
Pucca <Pu***@discussi ons.microsoft.c omwrote:
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.Drop DownItems has 0
item in its collection at the time of the code. Thanks.

An unhandled exception of type 'System.Argumen tOutOfRangeExce ption' occurred
in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and
less than the size of the collection.
ToolStripItemCo llection contextsMenuIte ms =
contextsMenuStr ip.Items;
.AddRange(conte xtsMenuItems);

..AddRange(cont extsMenuItems) 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.co m>
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***@discussi ons.microsoft.c omwrote:
I want to Change the dropdown menuItems from the menustrip's 1st item
(Action) to be the same as the contextmenustri p 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.co m>
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_NodeMous eClick(object sender,
TreeNodeMouseCl ickEventArgs e)
{
int index = 0;
string nodeTxt = e.Node.Text;
ToolStripMenuIt em actionMenu = (ToolStripMenuI tem)mainMenu.It ems[0];
actionMenu.Drop DownItems.Clear ();
if (nodeTxt != null)
{
switch (nodeTxt)
{
case "Computers" :
ToolStripItemCo llection computersMenuIt ems =
contextsMenuStr ip.Items;
actionMenu.Drop DownItems.AddRa nge(computersMe nuItems);
if (e.Button.ToStr ing() == "Right")
RefreshHelpMenu Strip.Show(ppTr ee, e.Location);
break;
}
}
--
Thanks.
"Jon Skeet [C# MVP]" wrote:
Pucca <Pu***@discussi ons.microsoft.c omwrote:
I want to Change the dropdown menuItems from the menustrip's 1st item
(Action) to be the same as the contextmenustri p 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.co m>
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***@discussi ons.microsoft.c omwrote in message
news:9E******** *************** ***********@mic rosoft.com...
OK, got it. Something like this for my future posting of codes. Thanks.
private void ppTree_NodeMous eClick(object sender,
TreeNodeMouseCl ickEventArgs e)
{
int index = 0;
string nodeTxt = e.Node.Text;
ToolStripMenuIt em actionMenu = (ToolStripMenuI tem)mainMenu.It ems[0];
actionMenu.Drop DownItems.Clear ();
if (nodeTxt != null)
{
switch (nodeTxt)
{
case "Computers" :
ToolStripItemCo llection computersMenuIt ems =
contextsMenuStr ip.Items;
actionMenu.Drop DownItems.AddRa nge(computersMe nuItems);
if (e.Button.ToStr ing() == "Right")
RefreshHelpMenu Strip.Show(ppTr ee, e.Location);
break;
}
}
--
Thanks.
"Jon Skeet [C# MVP]" wrote:
>Pucca <Pu***@discussi ons.microsoft.c omwrote:
I want to Change the dropdown menuItems from the menustrip's 1st item
(Action) to be the same as the contextmenustri p 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.co m>
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.Argument OutOfRangeExcep tion 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***@discussi ons.microsoft.c omschreef in bericht
news:A8******** *************** ***********@mic rosoft.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.Drop DownItems has 0
item in its collection at the time of the code. Thanks.

An unhandled exception of type 'System.Argumen tOutOfRangeExce ption'
occurred
in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and
less than the size of the collection.
ToolStripItemCo llection contextsMenuIte ms =
contextsMenuStr ip.Items;
.AddRange(conte xtsMenuItems);
--
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.Argument OutOfRangeExcep tion 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***@discussi ons.microsoft.c omschreef in bericht
news:A8******** *************** ***********@mic rosoft.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.Drop DownItems has 0
item in its collection at the time of the code. Thanks.

An unhandled exception of type 'System.Argumen tOutOfRangeExce ption'
occurred
in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and
less than the size of the collection.
ToolStripItemCo llection contextsMenuIte ms =
contextsMenuStr ip.Items;
.AddRange(conte xtsMenuItems);
--
Thanks.


Nov 6 '06 #8

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

Similar topics

1
4052
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
2676
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
3993
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 nothing in IE. I'd be greatful for any assistance. Also, if I will have problems the code on Opera or Safari, I'd appreciate any pointers--I don't have a Mac to test Safari. THanks very much, Michael
4
3486
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. Thanks, Dave
4
8467
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
8901
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 Me.ListBoxPerson.Items.AddRange(ary)
3
9929
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 (SP1) installed. The error is: System.Runtime.InteropServices.COMException(0x800A03EC): Exception from HRESULT: 0x800A03EC. at Microsoft.Office.Interop.Excel._Worksheet.Paste(Object Destination, Object Link) at...
2
4451
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 statistical purposes. I've been using Here's the situation: I have two main tables:
1
3016
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 COM context 0x17aeb8 to COM context 0x17abd8 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages.
0
10542
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10289
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9119
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7600
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6840
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5496
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2968
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.