C # delegate events using the pass value (writeup) between form

introduction:

  Forms pass between the value of each learning WinForm newbie FAQs, the most basic way is to first get to in a form to accept the value of the form. Then. To get a space or property, direct assignment, this place needs to receive the Form property or space must be public, is extremely unsafe way. It is to define a method and then assigned to a control or property by calling the method in a form acceptable to upgrade my novice in practice. Both methods are low-level non-secure manner. Very not recommended. In this article we talk about how to solve the problem of inter-passing form information delegate event (non-commissioned pure).

Scenes:

               By clicking the primary main form From_Main send button, all exhibit subform content transmission, as FIG.

 

 

Learning premise:

               Preliminary understanding of the delegate. If you do not understand the point of the commission, I recommended a look at the article on the basis of trust introductory article before himself.

text:

Step 1: Given a delegate 

Code:

      public delegate void SendMessageToChildForms (string s); // definition of a parameter string, delegate no return value, called SendMessageToChildForms.

Description: 1 where he can be defined in the project can be accessed in any (e.g., the main form, a subform, or a class). However, there has been defined in the form in this example the main form Namespace title in principle.

          2. The system has two built-defined delegate types Action and Func, you can omit this step directly. In step 3 instantiated him on it. Wherein Action delegate is no return value, Func is entrusted with a return value. The number of parameters to their inputs are 0 ~ 16.

Step 2: instantiate a delegate of this type of event

Code:

    public event SendMessageToChildForms smtcf_event; 

Note: 1 can omit the event, public sometimes does not write, can be abbreviated as SendMessageToChildForms smtcf; not conducive for beginners to understand, in fact, he is essentially an example of an event event.

   2. If we use Microsoft's built-in Action or Func delegate type can be written in such public event Action <string> SendMessageToChildForms; this string must be written, so the first step because there is no need to specify this parameter list.

           3. The relationship between the commission and the commission events, in this case using a delegate event, relative to delegate more secure. Lower coupling. A delegate type, class SendMessageToChildForms e.g., an instance of an event such as a delegate type smtcf_event.

The method defined specific implementation: Step 3 

Code:

void ToShowGetMessage public (String S)
{
     this.lb_ received content = S .Text;
}

Description: 1. The specific function method, where the forms need to be performed to write in a form in which, for example in this case is to update the child form of display, so write the child form, if replaced by another scene: child delete the current form of this information needs to be updated, then the definition of the main form 

           2. The method of parameter list and return type must be fully consistent with the commission. For example the present embodiment, the parameter list is a string message string, empty void return type, the method must be public;

Step 4: The method to bind to the commission

Code: 

= New new Form_Child frm_child Form_Child ();
smtcf_event + = new new SendMessageToChildForms (frm_child.ToShowGetMessage); // bind subform method on a specific instance of a delegate event
frm_child.Show ();

= New new Form_Child frm_child2 Form_Child ();
smtcf_event + = new new SendMessageToChildForms (frm_child2.ToShowGetMessage); // bind subform method on a specific instance of a delegate event
frm_child2.Show ();

Note: To execute a method bound to Him defined delegate on the type of event, time must be in the child form Show () when a new form initialization before

           2. A delegate event smtcf can bind multiple methods.

Step 5: Trigger commission

Code:

// commissioned Step 5: Trigger commission
if (! Smtcf_event = null) // determine whether the event delegate is empty, if the commission did not execute empty
{
smtcf_event.Invoke (this.textBox1.Text.Trim ()); // Invoke may be omitted abbreviated as smtcf (this.textBox1.Text.Trim ());
}

Description: 1. The commission must first determine what the event smtcf is empty. Then execute;

      2. General abbreviated as smtcf_event (this.textBox1.Text.Trim ());

          3. Some say the Internet delegate event smtcf_event not call Invoke () method directly. In this example the test may have reason to know where to ask of you.

 

Guess you like

Origin www.cnblogs.com/arcticfish/p/12297675.html