C# dynamically add event handler
Asked Answered
D

6

25

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 :(

Decarbonize answered 7/10, 2009 at 13:18 Comment(0)
M
44

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);
}    
Mahout answered 7/10, 2009 at 13:25 Comment(3)
I thought you wanted LastChild to show up in the messagebox? That would show FirstChild?Whisk
I don't know if i;m imagining things but I foudn it works also even without the "new EventHandler, just with e.g. item.Click+=itemClick; I just tried adding a panel to a form and doing panel.Click+=p_click; idea from wonky79 here #17047855Toritorie
@Toritorie sure you got it right. probably when this was originally answered that way of adding eventhandlers was not there or I just put it for regular way of doing it. item.Click += itemClick is syntactic sugar for expression in answer.Mahout
W
6

You could use the Tag property of the ToolStripMenuItem:

item.Tag = Announcements[i].LastChild.InnerText;

public void item_click(object sender, EventArgs e)
{
    var menu = sender as ToolStripMenuItem;
    if (menu!= null)
        MessageBox.Show(menu.Tag);
}

Or you could use a lambda, which will capture the variable:

string data = Announcements[i].LastChild.InnerText;
item.Click += (s, e) => { MessageBox.Show(data); };
Whisk answered 7/10, 2009 at 13:30 Comment(1)
I founded so helpful with the Lambda, that was i was searching for, not to create another method, just one line solution.Kamikamikaze
M
2

Well, if I understand your question correctly, your "needs help" section should become this:

item.Click += new EventHandler(item_click);

then you just need to add a function to your class:

public void item_click(object sender, EventArgs e)
{
   //do stuff here
}
Morganstein answered 7/10, 2009 at 13:22 Comment(2)
yea but thats where i get stuck. how do i send the data from the xml LastChild to that function?Decarbonize
Ah - I didn't realize that part - although you can already see the answer has been given - when you create an event handler the object that spawns the event (or is clicked on in this case) is the sender parameter. In general you just need to cast it to the right data type and go on your merry way.Morganstein
C
2

are you asking for the signature for the click event? if you're working in visual studio, you should be able to type

item.Click+= tab tab

and it'll generate something for you

Cagle answered 7/10, 2009 at 13:24 Comment(0)
S
1

For people who stumble across this question now:

I simply do this:

public Form1() {
item.Click += item_Click;
}

private void item_Click(object sender, EventArgs e) {
// Code in here
}

Visual Studio can save you the hassle, though. After you type item.Click +=, it'll auto-populate once you press tab.

Example

Silvereye answered 28/7, 2021 at 12:55 Comment(0)
L
0

I would recommend you look into subscriptions for events. In the event you have to make sure it's the last item in the menu item.
Look at MSDN's help for the item

Lemkul answered 7/10, 2009 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.