A delegate can pass a method as a parameter.
Public Delegate void Dgfillareas (string IDs,string names); // Defining Delegates // defining a delegate is a way to determine the type of signature (return value/parameter) that can be stored in this delegate object. // If this delegate is a description of this type of delegate object in the future can only store no return value has two string parameter method, the delegate can store multiple such signature method
Private voidBtnchoice_click (Objectsender, EventArgs e) {frmareas frm=NewFrmareas (Fillareas);//passing a method as a parameterfrm. ShowDialog (); } Public voidFillareas (stringIdsstringNames//the concrete implementation of the Entrustment method { This. Txtdistrict.text =names; This. Txtdistrict.tag =IDs; }
Dgfillareas dgfrm; // define a delegate type Public Frmareas (Dgfillareas dgfrm) // Secondary form constructor, accepts the delegate { InitializeComponent () returned from the main form; this. dgfrm = dgfrm;
Dgfrm. Invoke (AIDS. ToString (). Substring (01), Anams. ToString (). Substring (01)); // invoking a Delegate method
The advantage of this method is that the method is passed from the main form to the secondary form as a parameter, so the secondary form can only invoke the delegate's method, not the public member of the main form.
Delegate implementation form delivery