[quoted text, click to view] <www.dir@gmail.com> wrote in message
news:1119888674.041449.296940@z14g2000cwz.googlegroups.com...
> Hi,
>
> I am adding dynamically context menu items like this:
>
> MenuItem newStudent = new MenuItem( student.Name );
> newStudent .Click += new System.EventHandler( this.StudentClicked );
>
> And my method :
>
> void StudentClicked ( object sender, System.EventArgs e )
> {
> }
>
> And It is working just fine.
> The problem is that I have only one method executed for every student
> the User clicks on the context menu. I need to add a parameter or
> something so when the StudentClicked method is executed I will know
> which MenuItem ( Student ) was clicked. Normally I would add a student
> number in the tag field, and when a the StudentClicked method is
> executed I would get the tag, parse the number and I know thick student
> was pressed on the menu. But MenuItems do not have tag.
>
> Please advise.
>
The "sender" indicates which MenuItem was clicked
void StudentClicked ( object sender, System.EventArgs e )
{
MenuItem selectedMenu = (MenuItem) sender;
// selectedMenu.Text contains the text of the selected student.Name
}
Another (and probably better) option would be to create a custom class that
inherits MenuItem and add additional properties like Tag to your custom menu
items and use that instead of the Text.
public class ExMenuItem : System.Windows.Forms.MenuItem
{
public ExMenuItem() {}
Object _Tag;
public Object Tag
{
get { return _Tag; }
set { _Tag = value; }
}
}
MenuItem newStudent = new ExMenuItem( student.Name );
newStudent .Click += new System.EventHandler( this.StudentClicked );