C # event example

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. drawing;
Using system. Data;
Using system. text;
Using system. Windows. forms;
Using system. IO;

/// <Summary>
/// Custom Thumbnail Control
/// </Summary>
Public class ctlpicturebox
{
Public Delegate void picclickhandler (Object sender, pictureargs E );
/// <Summary>
/// Define a click event
/// </Summary>
Public event picclickhandler picclick;

Protected virtual void onpictureclick (pictureargs E)
{// Event trigger Method
If (picclick! = NULL)
{// Determine whether the event is empty
Picclick (this, e); // trigger the event
}
}
// Trigger event
Private void pbimg_click (Object sender, eventargs E)
{
Pictureargs ARGs = new pictureargs ();
Args. filename = filename;
Onpictureclick (ARGs );
}

}


Public class pictureargs: eventargs
{
Private string filename = "";
/// <Summary>
/// File name
/// </Summary>
Public String filename
{
Get {return filename ;}
Set {filename = value ;}
}
}

 

Example 2

 

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. drawing;
Using system. Data;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;

Namespace Test
{
Public partial class mycontrol: usercontrol
{
Public mycontrol ()
{
Initializecomponent ();
}

Public Delegate int mydelegate (string Str );

Public mydelegate clickhandler = NULL;

Private void button#click (Object sender, eventargs E)
{
If (clickhandler! = NULL)
{
Clickhandler ("hello ");
}
}
}
}

 

Call Method

Public int test (string Str)
{
MessageBox. Show (STR );
Return 1;
}

Public int test1 (string Str)
{
MessageBox. Show (STR + "," + Str );
Return 1;
}

Private void form1_load (Object sender, eventargs E)
{
Mycontrol1.clickhandler + = test;
Mycontrol1.clickhandler + = test1;

// Mycontrol1.clickhandler-= test1;
}

 

 

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.