Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

Add MenuItems Dynamically

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MWS - 31 Jul 2007 17:03 GMT
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++;
}
Ged - 31 Jul 2007 17:15 GMT
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

> Hello,
>
[quoted text clipped - 25 lines]
> TransCount++;
> }
MWS - 31 Jul 2007 19:25 GMT
> Are you sure that TransCount in your foreach loop has not exceeded 5 ?
> It looks like it has, which is causing the exception.
[quoted text clipped - 37 lines]
>
> - 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});
MWS - 31 Jul 2007 20:30 GMT
> > Are you sure that TransCount in your foreach loop has not exceeded 5 ?
> > It looks like it has, which is causing the exception.
[quoted text clipped - 56 lines]
>
> - 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...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.