Add MenuItems Dynamically

M

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++;
}
 
G

Guest

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
 
M

MWS

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




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});
 
M

MWS

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

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...
 

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