473,785 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loop through MenuItems

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
Nov 15 '05 #1
1 6739

"Steve Tan" <st********@yah oo.com> skrev i meddelandet
news:95******** *************** ***@posting.goo gle.com...
1. How can I loop through all the items and subitems for Menu of a
Windows Form (c#) ? Recursive function?
That's one possibility. Send the MenuItems from your MainMenu to this
example:

private void menuItems(Menu. MenuItemCollect ion mc)
{
foreach (MenuItem m in mc)
{
// Do whatever you like with
// the "single" item...

if (m.IsParent)
{
menuItems(m.Men uItems);
}
}
}

2. How can I uniquely identify a submenu item ?

MenuItems doesn't have a Name property.

How you need to identify a submenu item is depending on what you need to do.
Each object can be identified by the value of it's reference.

If you have more than one item with the same value in the Text-property,
maybe it should behave similar anyway.

If you absolutely need to have a Name property you can easily subclass the
MenuItem-class to your own specialized class.

class NamedMenuItem : MenuItem
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}

public NamedMenuItem () : base()
{
this.name = this.Text;
}
}

// Bjorn A

Nov 15 '05 #2

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

Similar topics

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: ...
14
6704
by: Yereth Jansen | last post by:
Hi all, I encountered a problem with looping through an associative array. All worked perfectly with the following code: for (var menuItem in this.menuItems) { doSomething(); } where this.menuItems is an associative array. The problem occurred when
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)
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 =...
5
1780
by: Jorge Cavalheiro | last post by:
Hello I am trying to browse thru all menuitens of contextmenus. The form has 3 contextmenus. mi and mi2 as menuitems, f is a form For Each mi In f.ContextMenu.MenuItems For Each mi2 In mi.MenuItems Next
8
5269
by: Dave | last post by:
I am trying to write an event-driven application with no main window that runs "forever". It waits on a named event and then displays a window depending on data pased in a memory-mapped file. I started with a standard console application and it all works fine, except that it does not terminate tidily when the user logs off/shuts down. So, I figured I needed a message loop to pick up WM_QUIT. I studied the code in "Waiting in a message loop"...
3
1388
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...
3
4839
by: MWS | last post by:
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}); The error that I'm getting is "ArgumentNullExceiption was unhandled"
0
9643
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
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
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...
0
10147
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...
0
8971
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
7496
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
5380
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2877
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.