Delegate to modify the control attributes in form in other classes

Source: Internet
Author: User

In general, we need to display the prompt information on the main interface from time to time in other business classes. The following method can be used:

Form1.cs

Using system;

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

Namespace windowsformsapplication2
{

Public partial class form1: Form
{

Public form1 ()
{
Initializecomponent ();
}

Private void form1_load (Object sender, eventargs E)
{
Testdelegate TD = new testdelegate ();
TD. cb = safesettext;
Thread th = new thread (New threadstart (TD. Test ));
Th. Start ();
}

Public void safesettext (string text)
{
If (this. invokerequired)
{
_ Safesettextcall call = delegate (string S)
{
This. label1.text = s;
};

This. Invoke (call, text );
}
Else
This. label1.text = text;
}

Public Delegate void _ safesettextcall (string text );
}
}

Testdelegate. CS

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Threading;

Namespace windowsformsapplication2
{
Public Delegate void callback (string MSG );

Public class testdelegate
{
Public callback CB;
Public testdelegate ()
{

}
Public void test ()
{
For (INT I = 0; I <100; I ++ ){
CB (I. tostring ());
Console. writeline (I. tostring ());
Thread. Sleep (1000 );
}
}
}

}

 

Delegate to modify the control attributes in form in other classes

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.