Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# dynamically add event handler

Tags:

Hi i have a simple question. here is my code:

        XmlDocument xmlData = new XmlDocument();         xmlData.Load("xml.xml");          /* Load announcements first */         XmlNodeList announcements = xmlData.GetElementsByTagName("announcement");          for (int i = 0; i < announcements.Count; i++)         {             ToolStripMenuItem item = new ToolStripMenuItem();              item.Name = announcements[i].FirstChild.InnerText;             item.Text = announcements[i].FirstChild.InnerText;              /* HERE IS WERE I NEED HELP */              item.Click += new EventHandler();              this.freedomMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { item });         } 

The xml LastChild holds information for each annoucement. I would like to create a click event handler where when teh list item is clicked, a message box shows up with the data inside it. My problem is i dont no how to dynamically generate event handlers to do this :(

like image 403
Tony Avatar asked Oct 07 '09 13:10

Tony


1 Answers

try:

 /* HERE IS WERE I NEED HELP */   item.Click += new EventHandler(toolStripClick); 

actual handler:

void toolStripClick(object sender, EventArgs e) {      ToolStripItem item = (ToolStripItem)sender;      MessageBox.Show(item.Text); }     
like image 78
TheVillageIdiot Avatar answered Sep 16 '22 23:09

TheVillageIdiot