C # solve the eventhandler parameter transfer problem using anonymous Functions

Source: Internet
Author: User

First, it is easy to dynamically generate picturebox,
Picturebox box = new picturebox ();
Box. imagelocation = imageroad;
Secondly, it is not difficult to add a right-click menu to picturebox,
Contextmenu menu = new contextmenu ();
Box. contextmenu = menu;
Then, add the "delete" item to the right-click menu and delete the image event. This is troublesome.
Menuitem item = new menuitem ("delete ");
Menu. menuitems. Add (item, new eventhandler (item_click ));
Void item_click (Object sender, eventargs E)
{
// Added the image deletion function.
// 1. Obtain the image object
// 2. delete an image
}
In the item_click method, you cannot delete an image object because it cannot obtain its value. Unless the picturebox object can be passed as a parameter in the item_click method. I don't know how to implement this.
Try to use the anonymous method,
Menu. menuitems. Add (item, delegate (Object o, eventhandler e) {box. imagelocation = "";});
Since the anonymous method is in the method body, box can be passed as a parameter.
Conclusion: The anonymous method is used to pass event parameters, because the anonymous method can use external parameters.
Keywords: event, Delegate, anonymous method
Language Platform: C #2.0 or above

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.