Use delegate to pass values between forms

Source: Internet
Author: User

We know that the second eventargs parameter of event can be used to pass data when an event is triggered. Today, it is used to pass values between forms.
The demo is winform.ProgramIs two forms, parentfrm and childfrm.

First define a class mycustomeventargs inherited from eventargs

Code
Public   Class Mycustomeventargs: eventargs // Event parameter class
{
Private   String _ STR;

Public mycustomeventargs ( string Str)
{< br> This . STR = STR; // set the attribute value
}

Public StringStr
{
Get{Return_ STR ;}
Set{_ Str=Value ;}
}
}

Then declare a delegate for processing the event

Code
 Public Delegate VoidMydelegate (ObjectSender, mycustomeventargs E );

Define another event

Code
 Public Static EventMydelegate myevent; (it may not be static)

Write in the load of parentfrm

Code
Myevent+ = NewMydelegate (parentfrm_myevent );

Write in the load of childfrm

Code
Parentfrm. myevent+ = NewMydelegate (childfrm_myevent );

There is a handler in childfrm.

Code
 VoidChildfrm_myevent (ObjectSender, mycustomeventargs E)
{
This. Textbox1.text=E. STR;
}

Then we can achieve the desired effect.

Source code:
Parentfrm, which contains a button, a textbox.

Code
Namespace Codeprojectwindowthread
{
Public   Delegate   Void Mydelegate ( Object Sender, mycustomeventargs E );

Public   Partial   Class Parentfrm: Form
{
Public   Static   Event Mydelegate myevent;
Public Parentfrm ()
{
Initializecomponent ();
}

Private VoidParentfrm_load (ObjectSender, eventargs E)
{
Myevent+ = NewMydelegate (parentfrm_myevent );

Childfrm FRM= NewChildfrm ();
FRM. Show ();
}

VoidParentfrm_myevent (ObjectSender, mycustomeventargs E)
{

}

Private   Void Button#click ( Object Sender, eventargs E)
{
Myevent (sender, New Mycustomeventargs ( This . Textbox1.text ));
}
}
Public   Class Mycustomeventargs: eventargs // Event parameter class
{
Private   String _ STR;

Public mycustomeventargs ( string Str)
{< br> This . STR = STR; // set the attribute value
}

Public StringStr
{
Get{Return_ STR ;}
Set{_ Str=Value ;}
}
}

 

ChildfrmCode, Which contains a textbox.

Code
Namespace Codeprojectwindowthread
{
Public   Partial   Class Childfrm: Form
{
Public Childfrm ()
{
Initializecomponent ();
}

private void childfrm_load ( Object sender, eventargs e)
{< br> parentfrm. myevent += New mydelegate (childfrm_myevent );
}

VoidChildfrm_myevent (ObjectSender, mycustomeventargs E)
{
This. Textbox1.text=E. STR;
}
}
}

 

Click the button in the parentfrm form. the textbox in the childfrm form displays the textbox value in the parentfrm form.

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.