Solved

Which dynamic created Menu Item is selected

Posted on 2004-08-26
2
219 Views
Last Modified: 2010-04-15
Ok I read a directory and determine how many files match my search criteria, then I add the names of these files to a menu. The idea being that when the item is selected in the menu that I will load it.

I was able to figure out a way to add the menu in a loop, but I can't figure out a way to find out which of these dynamic menus has been selected.

for(int i;i<#offiles;i++)
EventHandler ev = new EventHandler(this.Item_clicked);
File.MenuItems.Add(new MenuItem(strfilename, ev));

private void Item_clicked(object sender, EventArgs e)
{
     ??? How do I figure out which item was selected ??
}
0
Comment
Question by:toby_lang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Accepted Solution

by:
eternal_21 earned 50 total points
ID: 11909369
If the method is called by the MenuItem.Click event, then the "sender" parameter will be the MenuItem, so you could use the code:

  if(sender is System.Windows.Forms.MenuItem)
  {
    System.Windows.Forms.MenuItem menuItem;
    menuItem = (System.Windows.Forms.MenuItem)sender;

    // Process code here, dependant of menuItem, eg: switch(menuItem.Text)...
  }
  else
  {
    //Item_clicked sender parameter was some other type of object
  }
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question