473,795 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Add MenuItems Dynamically

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

transToolStripM enuItem.DropDow nItems.AddRange (new ToolStripItem[] {
TransMenuItem[TransCount]});

The error that I'm getting is "ArgumentNullEx ceiption 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 ToolStripMenuIt ems. I need to loop
though the app.config and declare the ToolStripMenuIt ems.


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

TransCount = 0;

foreach (string keyname in settings.Allkey s){
transToolStripM enuItem.DropDow nItems.AddRange (new ToolStripItem[] {
TransMenuItem[TransCount]});
TransMenuItems[TransCount].Name = "XXX";
TransMenuItems[TransCount].Text = "XXX";
TransCount++;
}

Jul 31 '07 #1
3 4839
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.AllKey s ?
You'll probably find there are more than 5

HTH

Ged

"MWS" <ma*******@hotm ail.comwrote in message
news:11******** *************@b 79g2000hse.goog legroups.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...

transToolStripM enuItem.DropDow nItems.AddRange (new ToolStripItem[] {
TransMenuItem[TransCount]});

The error that I'm getting is "ArgumentNullEx ceiption 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 ToolStripMenuIt ems. I need to loop
though the app.config and declare the ToolStripMenuIt ems.


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

TransCount = 0;

foreach (string keyname in settings.Allkey s){
transToolStripM enuItem.DropDow nItems.AddRange (new ToolStripItem[] {
TransMenuItem[TransCount]});
TransMenuItems[TransCount].Name = "XXX";
TransMenuItems[TransCount].Text = "XXX";
TransCount++;
}
Jul 31 '07 #2
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.AllKey s ?
You'll probably find there are more than 5

HTH

Ged

"MWS" <mattst...@hotm ail.comwrote in message

news:11******** *************@b 79g2000hse.goog legroups.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...
transToolStripM enuItem.DropDow nItems.AddRange (new ToolStripItem[] {
TransMenuItem[TransCount]});
The error that I'm getting is "ArgumentNullEx ceiption 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 ToolStripMenuIt ems. I need to loop
though the app.config and declare the ToolStripMenuIt ems.
//TransCount is an integer that equals 5 at this point
ToolStripMenuIt em[] TransMenuItems = new
ToolStripMenuIt em[TransCount];
TransCount = 0;
foreach (string keyname in settings.Allkey s){
transToolStripM enuItem.DropDow nItems.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...

transToolStripM enuItem.DropDow nItems.AddRange (new ToolStripItem[]
{TransMenuItem[TransCount]});

with...

ToolStripMenuIt em TransMenuItem = new ToolStripMenuIt em;
transToolStripM enuItem.DropDow nItems.AddRange (new ToolStripItem[]
{TransMenuItem} );
Jul 31 '07 #3
MWS
On Jul 31, 2:25 pm, MWS <mattst...@hotm ail.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.AllKey s ?
You'll probably find there are more than 5
HTH
Ged
"MWS" <mattst...@hotm ail.comwrote in message
news:11******** *************@b 79g2000hse.goog legroups.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...
transToolStripM enuItem.DropDow nItems.AddRange (new ToolStripItem[] {
TransMenuItem[TransCount]});
The error that I'm getting is "ArgumentNullEx ceiption 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 ToolStripMenuIt ems. I need to loop
though the app.config and declare the ToolStripMenuIt ems.
//TransCount is an integer that equals 5 at this point
ToolStripMenuIt em[] TransMenuItems = new
ToolStripMenuIt em[TransCount];
TransCount = 0;
foreach (string keyname in settings.Allkey s){
transToolStripM enuItem.DropDow nItems.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...

transToolStripM enuItem.DropDow nItems.AddRange (new ToolStripItem[]
{TransMenuItem[TransCount]});

with...

ToolStripMenuIt em TransMenuItem = new ToolStripMenuIt em;
transToolStripM enuItem.DropDow nItems.AddRange (new ToolStripItem[]
{TransMenuItem} );- Hide quoted text -

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

foreach (string keyname in settings.Allkey s){

TransMenuItems[TransCount] = new ToolStripMenuIt em();

transToolStripM enuItem.DropDow nItems.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
11330
by: Venkatesh | last post by:
Hi Everybody, This is the first timw I am entering into this Group. I am developing a VB Project with an MDI form. I want to display IE Favorites into my application. For this I need to create Submenus and Menuitems dynamically. It is possible to create menus using array index. But if I have multiple folders and files in Favorites then using array index will not work.
0
1289
by: Dan H | last post by:
I'm trying to add menu items dynamically in the Popup event of a MenuItem in an MDI child form. Actually, I'm using the Genghis MRU class, but have also dumbed it down to a simple test case in a standard WinForms app. When trying to call mnuItemFoo.MenuItems.Add( "some text" ) in the Popup handler, I get a null reference exception. System.NullReferenceException Object reference not sent to an instance of an object. It's weird - the...
0
1358
by: Deutsche Dogge | last post by:
Hi. I'M having a weird problem with addign menuitems to a menu dynamically on the popup event of the menuitem containing the container of the menuitems i wanna add (tools -> external -> {list of items dynamically added}) All the code works #1 when only the MdiParent form is used, as soon as i open an MdiChild in it, right after the popup event returns, i get this error: ...
0
1176
by: Kreisquadratur | last post by:
Below is s snippet of my code, which should add menuitems as a submenu of an item in a contextmenu. But only the first time the contextmenu opens the entrys are set correctly. I just don't know why or how to solve this problem... private void contextMenu1_Popup(object sender, System.EventArgs e) { contextMenu1.MenuItems.MenuItems.Clear(); Process myProcesses = Process.GetProcessesByName("IEXPLORE"); if (myProcesses.Length > 0)
1
6743
by: Steve Tan | last post by:
1. How can I loop through all the items and subitems for Menu of a Windows Form (c#) ? Recursive function? 2. How can I uniquely identify a submenu item ? MenuItems doesn't have a Name property. Thanks, Steve
0
1637
by: Brian | last post by:
Create a simple MDI form and a child form, and try the following simple code to add a new menu item on the Edit menu popup: private void editMenu_Popup(object sender, System.EventArgs e) { editMoreMenu.MenuItems.Add("New MI " + count); count++; } and get "An unhandled exception of type 'System.NullReferenceException'
5
2024
by: Davie | last post by:
I'm developing an application on the smartphone. To reduce the size of the application i'm using panels where appropriate instead of forms. As a result of the design, I need to have different context menus on one form. I had tried to declare two MainMenu items and then simply switch the display accordingly. eg. public void displayPanel1(){ this.Menu = this.mainMenu1; // this.Refresh(); } public void displayPanel2(){ this.Menu =...
3
1389
by: active | last post by:
I have a mainmenu menuitem that contains a Recently Used Files menuitem If at design time I include a menuitem in the Recently Used Files menuitem collection it will display OK If I programmatically add more menuitems to the Recently Used Files menuitem they show ok However, if I clear the Recently Used Files menuitem collection and then add more menuitems they do not display. What is weird is that even though they do not display the...
6
34686
by: Luqman | last post by:
How can I dynamically Add menuStrip and its menuItems through code using VB.Net 2005 ? Best Regards, Luqman
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10214
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
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
10001
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
9042
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
7538
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
5437
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...
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
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.