Solved

Which dynamic created Menu Item is selected

Posted on 2004-08-26
2
207 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

791 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