By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,634 Members | 1,897 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,634 IT Pros & Developers. It's quick & easy.

Add MenuItems Dynamically

P: n/a
MWS
Hello,

I'm trying to loop through the app.Config file and add the keys from
the app.Config as MenuItems on a MenuStrip. The following line blows
up...

transToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] {
TransMenuItem[TransCount]});

The error that I'm getting is "ArgumentNullExceiption was unhandled"
because TransMenuItem[TransCount] is null. Could someone please post a
code snippet out there with an example of how to dynamically add
menuItems to a menustrip? I've seen examples, but all of them have
hard coded declairations of the ToolStripMenuItems. I need to loop
though the app.config and declare the ToolStripMenuItems.


//TransCount is an integer that equals 5 at this point
ToolStripMenuItem[] TransMenuItems = new
ToolStripMenuItem[TransCount];

TransCount = 0;

foreach (string keyname in settings.Allkeys){
transToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] {
TransMenuItem[TransCount]});
TransMenuItems[TransCount].Name = "XXX";
TransMenuItems[TransCount].Text = "XXX";
TransCount++;
}

Jul 31 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Are you sure that TransCount in your foreach loop has not exceeded 5 ?
It looks like it has, which is causing the exception.

How many items are there in settings.AllKeys ?
You'll probably find there are more than 5

HTH

Ged

"MWS" <ma*******@hotmail.comwrote in message
news:11*********************@b79g2000hse.googlegro ups.com...
Hello,

I'm trying to loop through the app.Config file and add the keys from
the app.Config as MenuItems on a MenuStrip. The following line blows
up...

transToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] {
TransMenuItem[TransCount]});

The error that I'm getting is "ArgumentNullExceiption was unhandled"
because TransMenuItem[TransCount] is null. Could someone please post a
code snippet out there with an example of how to dynamically add
menuItems to a menustrip? I've seen examples, but all of them have
hard coded declairations of the ToolStripMenuItems. I need to loop
though the app.config and declare the ToolStripMenuItems.


//TransCount is an integer that equals 5 at this point
ToolStripMenuItem[] TransMenuItems = new
ToolStripMenuItem[TransCount];

TransCount = 0;

foreach (string keyname in settings.Allkeys){
transToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] {
TransMenuItem[TransCount]});
TransMenuItems[TransCount].Name = "XXX";
TransMenuItems[TransCount].Text = "XXX";
TransCount++;
}
Jul 31 '07 #2

P: n/a
MWS
On Jul 31, 12:15 pm, <Gedwrote:
Are you sure that TransCount in your foreach loop has not exceeded 5 ?
It looks like it has, which is causing the exception.

How many items are there in settings.AllKeys ?
You'll probably find there are more than 5

HTH

Ged

"MWS" <mattst...@hotmail.comwrote in message

news:11*********************@b79g2000hse.googlegro ups.com...
Hello,
I'm trying to loop through the app.Config file and add the keys from
the app.Config as MenuItems on a MenuStrip. The following line blows
up...
transToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] {
TransMenuItem[TransCount]});
The error that I'm getting is "ArgumentNullExceiption was unhandled"
because TransMenuItem[TransCount] is null. Could someone please post a
code snippet out there with an example of how to dynamically add
menuItems to a menustrip? I've seen examples, but all of them have
hard coded declairations of the ToolStripMenuItems. I need to loop
though the app.config and declare the ToolStripMenuItems.
//TransCount is an integer that equals 5 at this point
ToolStripMenuItem[] TransMenuItems = new
ToolStripMenuItem[TransCount];
TransCount = 0;
foreach (string keyname in settings.Allkeys){
transToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] {
TransMenuItem[TransCount]});
TransMenuItems[TransCount].Name = "XXX";
TransMenuItems[TransCount].Text = "XXX";
TransCount++;
}- Hide quoted text -

- Show quoted text -
That's not the problem. when I use watch window in the debugger
TransMenuItems[TransCount] is null. I'm guessing I'm somehow not using
the array correctly because I can get it to work if I replace...

transToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[]
{TransMenuItem[TransCount]});

with...

ToolStripMenuItem TransMenuItem = new ToolStripMenuItem;
transToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[]
{TransMenuItem});
Jul 31 '07 #3

P: n/a
MWS
On Jul 31, 2:25 pm, MWS <mattst...@hotmail.comwrote:
On Jul 31, 12:15 pm, <Gedwrote:


Are you sure that TransCount in your foreach loop has not exceeded 5 ?
It looks like it has, which is causing the exception.
How many items are there in settings.AllKeys ?
You'll probably find there are more than 5
HTH
Ged
"MWS" <mattst...@hotmail.comwrote in message
news:11*********************@b79g2000hse.googlegro ups.com...
Hello,
I'm trying to loop through the app.Config file and add the keys from
the app.Config as MenuItems on a MenuStrip. The following line blows
up...
transToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] {
TransMenuItem[TransCount]});
The error that I'm getting is "ArgumentNullExceiption was unhandled"
because TransMenuItem[TransCount] is null. Could someone please post a
code snippet out there with an example of how to dynamically add
menuItems to a menustrip? I've seen examples, but all of them have
hard coded declairations of the ToolStripMenuItems. I need to loop
though the app.config and declare the ToolStripMenuItems.
//TransCount is an integer that equals 5 at this point
ToolStripMenuItem[] TransMenuItems = new
ToolStripMenuItem[TransCount];
TransCount = 0;
foreach (string keyname in settings.Allkeys){
transToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] {
TransMenuItem[TransCount]});
TransMenuItems[TransCount].Name = "XXX";
TransMenuItems[TransCount].Text = "XXX";
TransCount++;
}- Hide quoted text -
- Show quoted text -

That's not the problem. when I use watch window in the debugger
TransMenuItems[TransCount] is null. I'm guessing I'm somehow not using
the array correctly because I can get it to work if I replace...

transToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[]
{TransMenuItem[TransCount]});

with...

ToolStripMenuItem TransMenuItem = new ToolStripMenuItem;
transToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[]
{TransMenuItem});- Hide quoted text -

- Show quoted text -
I never said I was smart.

foreach (string keyname in settings.Allkeys){

TransMenuItems[TransCount] = new ToolStripMenuItem();

transToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] {
TransMenuItem[TransCount]});
TransMenuItems[TransCount].Name = "XXX";
TransMenuItems[TransCount].Text = "XXX";
TransCount++;

I was missing the declairation in my loop...
Jul 31 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.